Merging JSON from two node outputs but got empty array

Describe the problem/error/question

Hi guys, sorry to bother you again, I’m trying to figure out how to merge a JSON output from one node into JSON as a nested array with a code node but I’m still getting empty property.

Does anyone have an idea of how to solve this? Also, this can be achieved with a merge node?

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

The expected output is this:

{
    "dte": {
        "datosGenerales": {
            "tipo": "FACT",
            "fechaHoraEmision": "2023-01-15T12:56:49.6090",
            "codigoMoneda": "GTQ"
        },
        "emisor": {
            "nitEmisor": "{{NIT Emisor}}",
            "nombreEmisor": "nombreEmisor",
            "codigoEstablecimiento": 1,
            "nombreComercial": "nombreComercial",
            "correoEmisor": "[email protected]",
            "afiliacionIVA": "GEN",
            "direccion": {
                "direccion": "direccionEmisor",
                "codigoPostal": 0,
                "municipio": "municipioEmisor",
                "departamento": "departamentoEmisor",
                "pais": "GT"
            }
        },
        "receptor": {
            "idReceptor": "CF",
            "nombreReceptor": "nombreReceptor",
            "correoReceptor": "[email protected]",
            "direccion": {
                "direccion": "direccionReceptor",
                "codigoPostal": 0,
                "municipio": "municipioReceptor",
                "departamento": "departamentoReceptor",
                "pais": "GT"
            }
        },
        "frases": [
            {
                "tipoFrase": 1,
                "codigoEscenario": 1
            }
        ],
        "items": [
            {
                "bienOServicio": "B",
                "cantidad": 1,
                "unidadMedida": "UN",
                "descripcion": "product demo",
                "precioUnitario": 5000,
                "precio": 5000,
                "descuento": 0,
                "total": 5000,
                "impuestos": [
                    {
                        "nombreCorto": "IVA",
                        "codigoUnidadGravable": 1,
                        "montoGravable": 4464.2857142857,
                        "montoImpuesto": 535.7142857142
                    },
 {
                "bienOServicio": "B",
                "cantidad": 1,
                "unidadMedida": "UN",
                "descripcion": "another product",
                "precioUnitario": 5000,
                "precio": 5000,
                "descuento": 0,
                "total": 5000,
                "impuestos": [
                    {
                        "nombreCorto": "IVA",
                        "codigoUnidadGravable": 1,
                        "montoGravable": 4464.2857142857,
                        "montoImpuesto": 535.7142857142
                    }
                ]
            }
        ]
}

Information on your n8n setup

  • n8n version: 0.231.2
  • Database (default: SQLite): PostgreSQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main): own
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: N/A

Hi @rodmontgt, in your example workflow the Code node would run twice. Once after your Dte node finished, and another time when your line_items node finishes.

For your Code node to run only once but have access to both input items, you would need to add a Merge node merging both branches first.

Hope this helps! If you’re still facing problems afterwards it’d be great if you could share the JSON data your are sending to each of the inputs of your Merge node so I can take a closer look.

Thanks @MutedJam ,This is the data received from the webhook, I have to paste it outside the forum because it doesn’t let me due to the size.
https://pastebin.mozilla.org/KRoDuW4u

I tried with the merge node but Im not getting the result that I’m expecting.

1 Like

Thank you for sharing this example dataset @rodmontgt! Seeing you’re using a webhook I assume you will only ever get one item at a time from your Dte node, making this project a little simpler. Here’s an example workflow transforming the data structure as you have described:

The methods I am using to read data from your previous nodes are documented here in case you’d like to learn more about the available options for reading data from previous nodes.

Hope this helps :slight_smile:

1 Like

Thank you very much @MutedJam, out of the box the code didn’t work, I have to make some adjustments but I really appreciate your help.

I was able to resolve the “issue” with the following code:

// Read data from Dte node
const dataDte = $('Dte').first().json;

dataDte.dte.items = $('line_items').all().map(e => e.json);

return [{
  json: dataDte
  }
];

Thanks again

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.