Extract values

Hi,
I would like to extract the values contained in this string

{"result":"[{\"indice\":1,\"pour_un_besoin_de\":1,\"je_suis_le_composant\":\"653005\",\"du_compose\":\"653005\",\"pour_la_semaine\":202205,\"mon_stock_debut_sem\":0,\"mon_dispo_calcul\":-20,\"mon_fabricable_pour_ce_compose\":-20,\"mon_stock_de_fabricable_depuis_mon_composant\":-20},{\"indice\":0,\"pour_un_besoin_de\":1,\"je_suis_le_composant\":653005,\"du_compose\":653005,\"pour_la_semaine\":202205,\"mon_stock_debut_sem\":0,\"mon_dispo_calcul\":-20,\"mon_fabricable_pour_ce_compose\":0,\"mon_stock_de_fabricable_depuis_mon_composant\":-20}]"}

I think it’s very simple but I can’t put my finger on it

Thanks to you

Hey @fporta, so instead of a single result field which is a string you’d like to have multiple items, each with fields like indice, pour_un_besoin_de, je_suis_le_composant etc.?

You’d essentially need to parse the string/convert it into a JSON object and split each separate result item up into an n8n element and move the fields within the result field to the top level of each item.

That’s a bit of custom logic required here, so I’d suggest using the Function node for this transformation. A snippet performing this transformation could look like so:

let results = [];

for (item of items) {
  for (result of JSON.parse(item.json.result)) {
    results.push({
      json: result
    });
  }
}

return results;

This would run through all input items, then through each parsed result and pushes it to a new results array following n8n’s data structure. Once done it returns the results:

Example Workflow

you’re a genius :wink: :wink:
thanks

1 Like

I take it it’s working then :smiley:

Glad to hear!