JSON manipulation from key-value to array?

Hi all,
I need to work on each key entry values (ean, image, name, …).
I need an array to be able to call an api with the split node. I try some JS snippet without succes.

Thanks for help

image

Hey @Christel_Doudet!

Can you share the structure of the input as well as the output you want? Do the keys ean, images, etc. contain multiple values? If not, do you want to convert them to a key-value (JSON) structure?

Hi,
thanks for reply
i need an array like this

Key Value
image [
{
data: a/3/a/a/a3aa7d927826cf52d8dc4bdad63e4cfe782bbb6c_bag.jpg,
_links: {
download: {
href: http://pim-poc.coreplighting.net/api/rest/v1/media-files/a/3/a/a/a3aa7d927826cf52d8dc4bdad63e4cfe782bbb6c_bag.jpg/download
}
},
scope: null,
locale: null
}
]
ean [
{
data: 1234567890183,
scope: null,
locale: null
}
]
price [
{
data: [
{
amount: 456.00,
currency: EUR
}
],
scope: null,
locale: null
}
]

/ex:
“values”: {

"image": [

{

"data": "a/3/a/a/a3aa7d927826cf52d8dc4bdad63e4cfe782bbb6c_bag.jpg",

"_links": {

"download": {

"href": "http://pim-poc.coreplighting.net/api/rest/v1/media-files/a/3/a/a/a3aa7d927826cf52d8dc4bdad63e4cfe782bbb6c_bag.jpg/download"
"values": {
    "image": [
        {
            "data": "a/3/a/a/a3aa7d927826cf52d8dc4bdad63e4cfe782bbb6c_bag.jpg",
            "_links": {
                "download": {
                    "href": "http://pim-poc.coreplighting.net/api/rest/v1/media-files/a/3/a/a/a3aa7d927826cf52d8dc4bdad63e4cfe782bbb6c_bag.jpg/download"
                }
            },
            "scope": null,
            "locale": null
        }
    ],
    "ean": [
        {
            "data": "1234567890183",
            "scope": null,
            "locale": null
        }
    ],
    "price": [
        {
            "data": [
                {
                    "amount": "456.00",
                    "currency": "EUR"
                }
            ],
            "scope": null,
            "locale": null
        }
    ],
    "name": [
        {
            "data": "Bag",
            "scope": null,
            "locale": null
        }
    ],
    "weight": [
        {
            "data": {
                "amount": "500.0000",
                "unit": "GRAM"
            },
            "scope": null,
            "locale": null
        }
    ]
}

Hey @Christel_Doudet!

Can you check the output that you have shared? It has a very different structure from the screenshot that you shared earlier.

Hi, the screen shot is an input. it’s the data to transform.
It’s the data of a http node from product data.
I have to duplicate this data to another data product manager.
In the taget i only can update attributes by attributes.
So my idea was to transform the object values on an array of its items. I can them split each row to call the target’s update api.

thanks

i’m close.
Here is a JS example, but i don’t understand how to deal with .json property in n8n.

var tab_string = '{"name":[{"locale":null,"scope":null,"data":"Avision AV36"}],"description":[{"locale":"de_DE","scope":"print","data":"Sammeln sich unterwegs zu viele Dokumente an?"},{"locale":"en_US","scope":"print","data":" This is the vision fully realized with the AV36, which is powered by a USB port instead of a wall socket."}],"release_date":[{"locale":null,"scope":"ecommerce","data":"2012-01-05T00:00:00+00:00"}],"color_scanning":[{"locale":null,"scope":null,"data":true}]}';
var tab_array = JSON.parse(tab_string);
var ret_array = [];
var i=0;
for (const property in tab_array){
    ret_array[i]={};
    ret_array[i].name=property;
    ret_array[i].value=tab_array[property];
    i=i+1;
}
var ret_json=JSON.stringify(ret_array);
console.log(ret_array);
console.log(ret_json);

it give :
[{“name”:“name”,“value”:[{“locale”:null,“scope”:null,“data”:“Avision AV36”}]},{“name”:“description”,“value”:[{“locale”:“de_DE”,“scope”:“print”,“data”:“Sammeln sich unterwegs zu viele Dokumente an?”},{“locale”:“en_US”,“scope”:“print”,“data”:" This is the vision fully realized with the AV36, which is powered by a USB port instead of a wall socket."}]},{“name”:“release_date”,“value”:[{“locale”:null,“scope”:“ecommerce”,“data”:“2012-01-05T00:00:00+00:00”}]},{“name”:“color_scanning”,“value”:[{“locale”:null,“scope”:null,“data”:true}]}]

Hey @Christel_Doudet!

What error do you receive? Every item in n8n requires a JSON field. The following code snippet might work for you,

for (const property in tab_array){
    ret_array,json[i]={};
    ret_array.json[i].name=property;
    ret_array.json[i].value=tab_array[property];
    i=i+1;
}

What I am doing is appending the json keyword before all the items. You can use the return statement return ret_array to return the values.

ok,
For noob like me, javascript in n8n and explanation of data structure

thanks @harshil1712

1 Like

not the most genius piece of code, but it works

var tab_array=[];
tab_array=items[0].json.values;
var ret_array = [];
var i=0;
var items2=[];
for (const property in tab_array){
    ret_array[i]={};
    items2[i]={};
    ret_array[i].name=property;
    ret_array[i].value=tab_array[property];
    items2[i].json=ret_array[i];
    i=i+1;
}

return items2;
2 Likes

That’s awesome! Thank you for sharing the solution :sparkling_heart: