How to convert an array of objects into a JSON array string for an HTTP query parameter?

:bullseye: What is your goal?

My goal is to retrieve product stock quantities from an external API.
To do this, I first retrieve a list of products, extract their IDs, and then pass those IDs to another endpoint that returns stock information.

The stock endpoint requires the parameter idProduct formatted exactly as a JSON array of strings like this:
["ID_1","ID_2","ID_3"]

This list must be passed as a query parameter (idProduct) in an HTTP request to an API endpoint that requires a JSON array of product IDs.

The IDs come from a previous HTTP module that returns a list of products.

:thinking: What is the problem & what have you tried?

The API returns a list of products with the structure:
data.products[].id

Example response:

  "data": {
    "products": [
      { "id": "c0999f42-af56-4960-bff9-394683763929" },
      { "id": "2c10f60f-7113-4085-a679-8acc3670c569" }
    ]
  }
}

I need to extract only the IDs and build the following string:
["c0999f42-af56-4960-bff9-394683763929","2c10f60f-7113-4085-a679-8acc3670c569"]

I attempted:

  • {{ "[" & "\"" & join(6.data.products[].id ; "\",\"" ) & "\"" & "]" }}
  • map(data.products; β€œid”)
  • Array Aggregator
  • concatenation with β€œ[” & join(…) & β€œ]”
  • i also tried using Array Iterator and Array Aggregator the separate only the ID but i don’t know now how to concatenate the id into a string in that form.

but Make either:

  • generates invalid IML errors
  • outputs nested arrays
  • or sends the formula as plain text instead of evaluating it.

The API only accepts this exact JSON array format for the idProduct parameter.
I just need to build this exact string of IDs to make an API call.
In the screenshot you can see the last attempt im trying to use, in the last HTTP module there is the API call that i need to use.

:camera_with_flash: Screenshots (scenario flow, module settings, errors)

Hey there,

You can try with an iterator and then aggregate to JSON. Or a create JSON module.

But having map() on the array on IDs inside a join() should give you a comma separated list like the one you described.

Thank for the help.
After this post i had i genius idea and i solve the problem.
I create a Set Variable, after the array aggregator, using this formula as value:
[β€œ{{join(map(126.array; β€œid”); β€œβ€β€,β€œβ€β€œ)}}”]

And now everything is working as expected, with this output:
[ { "id": "[\"c0999f42-af56-4960-bff9-394683763929\",\"2c10f60f-7113-4085-a679-8acc3670c569\",\"ca6df26e-9694-4bee-bf34-ee2698249506\",\"7e9d95a4-c401-4daf-839c-dfc9838f73e3\",\"0750a201-560e-4062-a2b1-6359b0400d66\"]" } ]

Thanks for the help anyway!!

1 Like