Json data manipulation with function

I am receiving some data via Webhook but the information is grouped

“body”: {

“data”: “{“retorno”:{“pedidos”:[{“pedido”:{“desconto”:“0,00”,“observacoes”:”",“observacaointerna”:"",“data”:“2021-12-08”,“numero”:“41”,“numeroOrdemCompra”:"",“vendedor”:"",“valorfrete”:“0.00”,“outrasdespesas”:“0.00”,“totalprodutos”:“39.90”,“totalvenda”:“39.90”,“situacao”:“Atendido”,“dataSaida”:“2021-12-08”,“loja”:“203298548”,“cliente”:{“id”:“4870766603”,“nome”:“Consumidor Final”,“cnpj”:null,“ie”:null,“rg”:"",“endereco”:null,“numero”:"",“complemento”:"",“cidade”:null,“bairro”:null,“cep”:null,“uf”:null,“email”:null,“celular”:null,“fone”:null},“pagamento”:{“categoria”:“Vendas (Gr\u00e1fica)”},“itens”:[{“item”:{“codigo”:"",“descricao”:“Impress\u00e3o Adesivo M\u00b2 Fosco”,“quantidade”:“1.0000”,“valorunidade”:“39.9000000000”,“precocusto”:null,“descontoItem”:“0.00”,“un”:“M\u00b2”,“pesoBruto”:“0.00000”,“largura”:“1”,“altura”:“1”,“profundidade”:"",“descricaoDetalhada”:"",“unidadeMedida”:“cm”,“gtin”:""}}],“parcelas”:[{“parcela”:{“idLancamento”:“15504385940”,“valor”:“39.90”,“dataVencimento”:“2021-12-08 00:00:00”,“obs”:"",“destino”:“3”,“forma_pagamento”:{“id”:“584666”,“descricao”:“Dinheiro”,“codigoFiscal”:“1”}}}]}}]}}"

}

I need the data to be returned this way in order to use the data

[image]

[

{

“retorno”: {

“pedidos”: [

{

“pedido”: {

“desconto”: “0,00”,

“observacoes”: “”,

“observacaointerna”: “”,

“data”: “2021-12-08”,

“numero”: “41”,

“numeroOrdemCompra”: “”,

“vendedor”: “”,

“valorfrete”: “0.00”,

“outrasdespesas”: “0.00”,

“totalprodutos”: “39.90”,

“totalvenda”: “39.90”,

“situacao”: “Atendido”,

“dataSaida”: “2021-12-08”,

“loja”: “203298548”,

“cliente”: {

“id”: “4870766603”,

“nome”: “Consumidor Final”,

“cnpj”: null,

“ie”: null,

“rg”: “”,

“endereco”: null,

“numero”: “”,

“complemento”: “”,

“cidade”: null,

“bairro”: null,

“cep”: null,

“uf”: null,

“email”: null,

“celular”: null,

“fone”: null

},

“pagamento”: {

“categoria”: “Vendas (Gráfica)”

},

“itens”: [

{

“item”: {

“codigo”: “”,

“descricao”: “Impressão Adesivo M² Fosco”,

“quantidade”: “1.0000”,

“valorunidade”: “39.9000000000”,

“precocusto”: null,

“descontoItem”: “0.00”,

“un”: “M²”,

“pesoBruto”: “0.00000”,

“largura”: “1”,

“altura”: “1”,

“profundidade”: “”,

“descricaoDetalhada”: “”,

“unidadeMedida”: “cm”,

“gtin”: “”

}

}

],

“parcelas”: [

{

“parcela”: {

“idLancamento”: “15504385940”,

“valor”: “39.90”,

“dataVencimento”: “2021-12-08 00:00:00”,

“obs”: “”,

“destino”: “3”,

“forma_pagamento”: {

“id”: “584666”,

“descricao”: “Dinheiro”,

“codigoFiscal”: “1”

}

}

}

]

}

}

]

}

}

]

I looked here for a way to remove the data via an array using a function, but unfortunately I can’t make it work

can you help me? Thank you very much

Hey @Rodrigo_Barbosa, this looks like the data is simply stringified. Would you be able to parse it using JSON.parse()? Here’s an example where I am using the Function Item node to do so:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n  json: {\n    body: {\n      data: \"{\\\"retorno\\\": \\\"...\\\"}\"\n    }\n  }\n}]"
      },
      "name": "Set Example Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "item.data = JSON.parse(item.body.data);\n\nreturn item;"
      },
      "name": "Parse JSON",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Example Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Example Data": {
      "main": [
        [
          {
            "node": "Parse JSON",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

If that’s not what you’re looking for, could you share your code snippets in preformatted blocks? You can either use this button:

image

Or manually put three backticks ``` above and below your code snippets. This would prevent the forum from applying formatting to your code which makes it really hard to use it.

Thank you!

1 Like

that was exactly it, thank you very much, it helped me a lot

[image]

1 Like

Glad to hear, thanks so much for confirming!

1 Like