Roket
1
Hey!
How do I convert JSON from the old structure to the new structure using the function node?
Old structure:
"meta_input":{
"first_custom_key": "first custom value",
"second_custom_key": "second custom value"
}
New structure:
"manage_meta_data":{
"update_post_meta":[
{
"meta_key": "first_custom_key",
"meta_value": "first custom value"
},
{
"meta_key": "second_custom_key",
"meta_value": "second custom value"
}
]
}
Check the example below:
{
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"typeVersion": 1,
"position": [
250,
300
]
},
{
"parameters": {
"functionCode": "return [\n {\n json: {\n \"meta_input\":{\n\t\"first_custom_key\": \"first custom value\",\n\t\"second_custom_key\": \"second custom value\"\n}\n }\n }\n]"
},
"name": "Function",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
450,
300
]
},
{
"parameters": {
"functionCode": "const data = items[0].json.meta_input \n\nconst postMeta = [];\n\nfor (const key of Object.keys(data)) {\n postMeta.push({\n meta_key: key,\n meta_value: data[key]\n })\n}\n\nreturn [\n {\n json: {\n manage_meta_data: {\n update_post_meta: postMeta\n }\n }\n }\n]"
},
"name": "Function1",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
650,
300
]
}
],
"connections": {
"Start": {
"main": [
[
{
"node": "Function",
"type": "main",
"index": 0
}
]
]
},
"Function": {
"main": [
[
{
"node": "Function1",
"type": "main",
"index": 0
}
]
]
}
}
}
1 Like
Roket
3
Ricardo, thank you very much!