Need to restructure/remove some incoming JSON array data

Just started diving into n8n from Integromat, so far love it, but am going a bit crazy with the documentation. I wish there was some documentation on the javascript notation needed to use the funtion nodes. What I have below is the output from a Set node.

Essentially, I need to take (could be any number of items, I’ve got two for the sake of this example):

[
   {
      "items":{
         "63":[
            {
               "id":646,
               "key":"pa_size",
               "value":"s"
            },
            {
               "id":647,
               "key":"Product API ID",
               "value":"777"
            },
            {
               "id":648,
               "key":"Artist",
               "value":"2"
            },
            {
               "id":649,
               "key":"files",
               "value":"link-to-file-1"
            }
         ],
         "64":[
            {
               "id":659,
               "key":"pa_size",
               "value":"xs"
            },
            {
               "id":660,
               "key":"Product API ID",
               "value":"5554"
            },
            {
               "id":661,
               "key":"Artist",
               "value":"2"
            },
            {
               "id":662,
               "key":"files",
               "value":"link-to-file-2"
            }
         ]
      },
       "name": "John Doe",
       "address1": "19749 Dearborn St",
       "city": "Chatsworth",
       "state_code": "CA",
       "country_code": "US",
       "zip": "91311"
   }
]

And turn it into:

{
    "recipient": {
        "name": "John Doe",
        "address1": "19749 Dearborn St",
        "city": "Chatsworth",
        "state_code": "CA",
        "country_code": "US",
        "zip": "91311"
    },
    "items": [{
        "Product API ID": 777,
        "quantity": 1,
        "files": [{
            "url": "link-to-file-1"
        },
        {
        "Product API ID": 5554,
        "quantity": 1,
        "files": [{
            "url": "link-to-file-2"
        }]
    }]
}

Any help would be awesome, or even just a link to an example or two. Other than a link to the document page for the function nodes, that doesn’t have enough context for me to wrap my head around this.

Thanks!

Welcome to the community @gravitoad!

You can find documentation about how the data in n8n looks like here:

Assuming that you just have one item, you can access the data In the Function-Node like this:

item[0].json.items // Will be your items. You can then simply iterate over them
item[0].json.name // Is the name
item[0].json.address1 // Is the address1
... // And so on

In the node, you can build a totally new item with the data you want. You just have to make sure you return it in the correct format and that it is an array. Basic example:

return [
  {
    json: {
      myString: 'teset'
   }
  }
];

That will result in a single item with the data: myString: "test"

So you can combine the both in something like this:

const newItem = {
  recipient: {
    name: item[0].json.name,
  },
  items: [],
};

newItem

return [
  {
    json: newItem
  }
];

So you just have to fill in the other data to recipient and items with regular JavaScript.

Hope that helps!

Awesome, that got me half way there, so I can build out the recipient part. But how do I dynamically convert the numbered arrays? I’ll always only need the second and fourth Value fields from those, and the number of how many arrays will change, as well as the array number title.