Add attribute:value in json on specific line

HI,
is it possible to insert a new line in a json at a specific place ?
here is the original json

{
"order_id": "123456",
"order_line_items/order_line_item/1/order_line_item_id": 978452,
"order_line_items/order_line_item/1/sku": "XXXXXXXXXXXXXX",
"order_line_items/order_line_item/1/quantity": 2,
"order_line_items/order_line_item/1/status": "CANCEL",
"order_line_items/order_line_item/2/order_line_item_id": 978453,
"order_line_items/order_line_item/2/sku": "XXXXXXXXXXXXXX",
"order_line_items/order_line_item/2/quantity": 2,
"order_line_items/order_line_item/2/status": "SHIPPED",
"order_line_items/order_line_item/3/order_line_item_id": 978451,
"order_line_items/order_line_item/3/sku": "XXXXXXXXXXXXXX",
"order_line_items/order_line_item/3/quantity": 1,
"order_line_items/order_line_item/3/status": "REFUND"
}

here is the result I want to have
{
“order_id”: “123456”,
"marketplace_id":“2020.000.000”,
“order_line_items/order_line_item/1/order_line_item_id”: 978452,
“order_line_items/order_line_item/1/sku”: “XXXXXXXXXXXXXX”,
“order_line_items/order_line_item/1/quantity”: 2,
“order_line_items/order_line_item/1/status”: “CANCEL”,
“order_line_items/order_line_item/2/order_line_item_id”: 978453,
“order_line_items/order_line_item/2/sku”: “XXXXXXXXXXXXXX”,
“order_line_items/order_line_item/2/quantity”: 2,
“order_line_items/order_line_item/2/status”: “SHIPPED”,
“order_line_items/order_line_item/3/order_line_item_id”: 978451,
“order_line_items/order_line_item/3/sku”: “XXXXXXXXXXXXXX”,
“order_line_items/order_line_item/3/quantity”: 1,
“order_line_items/order_line_item/3/status”: “REFUND”
}

Thanks

Hey @fporta,

If you want to do it in a no-code way, then you can use the Set node. But I am curious to know why you want to add it to a specific line. Can you help me understand it?

@harshil1712
I understand that my question is a bit strange. Here is my process.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -3180,
        -20
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "select marketplace, order_id, marketplace_id, tracking_url, tracking_number, carrier from commande_entete",
        "additionalFields": {
          "queryParams": "status='62' AND retour_info_site = 'N'"
        }
      },
      "name": "Postgres",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        -2840,
        -20
      ],
      "credentials": {
        "postgres": "Postgres_Diatly"
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "select   order_id,marketplace_id,order_line_item_id, sku,  quantity, status from commande_ligne\n",
        "additionalFields": {
          "queryParams": "=marketplace= {{$node[\"Postgres\"].json[\"marketplace\"]}} and order_id = {{$node[\"Postgres\"].json[\"order_id\"]}} and marketplace_id = {{$node[\"Postgres\"].json[\"marketplace_id\"]}}"
        }
      },
      "name": "Postgres1",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        -2570,
        -20
      ],
      "credentials": {
        "postgres": "Postgres_Diatly"
      }
    },
    {
      "parameters": {
        "functionCode": "const response = {};\nvar lib = 'order_line_items/order_line_item/';\nvar sep = '/';\nfor (const [index, item] of items.entries()) {\n    for (const key of Object.keys(item.json)) {\n      if (key === 'order_id') {\n        response[key] = item.json[key];\n      } else {\n        response[`${lib}${index + 1}${sep}${key}`] = item.json[key]\n      }\n    }\n}\n\nreturn [\n  {\n    json: response\n  }\n]\n"
      },
      "name": "Function_pivot",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -2280,
        -20
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres": {
      "main": [
        [
          {
            "node": "Postgres1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres1": {
      "main": [
        [
          {
            "node": "Function_pivot",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

in the node fonction_pivot, I don’t know how to get a compound key (key === ‘order_id’ + ‘marketplace_id’) and not have the repetition of the marketplace_id value in the result.

[
{
"order_id": "123456",
"order_line_items/order_line_item/1/marketplace_id": "2020.000.0000000",
"order_line_items/order_line_item/1/order_line_item_id": 978452,
"order_line_items/order_line_item/1/sku": "XXXXXXXXXXXXXX",
"order_line_items/order_line_item/1/quantity": 2,
"order_line_items/order_line_item/1/status": "CANCEL",
"order_line_items/order_line_item/2/marketplace_id": "2020.000.0000000",
"order_line_items/order_line_item/2/order_line_item_id": 978453,
"order_line_items/order_line_item/2/sku": "XXXXXXXXXXXXXX",
"order_line_items/order_line_item/2/quantity": 2,
"order_line_items/order_line_item/2/status": "SHIPPED",
"order_line_items/order_line_item/3/marketplace_id": "2020.000.0000000",
"order_line_items/order_line_item/3/order_line_item_id": 978451,
"order_line_items/order_line_item/3/sku": "XXXXXXXXXXXXXX",
"order_line_items/order_line_item/3/quantity": 1,
"order_line_items/order_line_item/3/status": "REFUND"
}
]```
 I was thinking of adding it in the output json.

thanks

Thanks for sharing more details. Are you fetching the marketplace_id from Postgres? In the input data you shared earlier, there is no marketplace_id. Also, can you explain what you mean by compound key? In the expected output, I don’t see order_id + marketplace_id.

@harshil1712
I found my answer, I’m a bit stupid at the moment. I modified the code in the pivot_function node.

for (const [index, item] of items.entries()) {
    for (const key of Object.keys(item.json)) {
      if (key === 'order_id') {
        response[key] = item.json[key];
        } else {
        response[`${lib}${index + 1}${sep}${key}`] = item.json[key]
      }
    }
}

by

for (const [index, item] of items.entries()) {
    for (const key of Object.keys(item.json)) {
      if ((key === 'order_id') || (key === 'marketplace_id')){
        response[key] = item.json[key];
        } else {
        response[`${lib}${index + 1}${sep}${key}`] = item.json[key]
      }
    }
}

Thank you very much for your help. Your question made me look at the problem from another point of view

1 Like

I am glad that I could be of help!

Have fun :slight_smile: