Complex JSON transformation

Have a node to extract data from a PDF form into JSON; each key/value pair looks like this:

[{ “LicenseInformation.LicenseYear”: [
“id”: “8R”,
“value”: “2022”,
“defaultValue”: “”,
“multiline”: false,
“password”: false,
“charLimit”: 4,
“comb”: false,
“editable”: true,
“hidden”: false,
“name”: “LicenseInformation.LicenseYear”,
“rect”: [
“actions”: null,
“page”: 0,
“strokeColor”: null,
“fillColor”: null,
“rotation”: 0,
“type”: “text”
], (…) }]

I need to transform this to “name”: “value” such that I can store it into MongoDB. So a function something like:

*[0].“name” = *[0].“value”

There are about 100 of these in a form so “Set” is not an option and JMESPath does not seem to iterate through keys…

Is there a straight forward way of doing this?

just had similar issue. easiest foolproof methot is to use function. some coding, but it works


You would indeed need a function node I think. That would at least be the easiest way of fixing it.
I am however not 100% sure what you need your output to be. Can you create a full example of the shown input?

1 Like

ok function it is, settling for:

var newItems = {};
for (const [key, value] of Object.entries(items[0].json)) {
// only needed if you use complex keys ie “key1.key2” for hierarchy
keyArr = key.split(‘.’)
switch (keyArr.length) {
case 1: newItems[keyArr[0]] = (value[0].value != null) ? value[0].value : {} ;
case 2: newItems[keyArr[0]][keyArr[1]] = (value[0].value != null) ? value[0].value : {} ;
case 3: newItems[keyArr[0]][keyArr[1]][keyArr[2]] = (value[0].value != null) ? value[0].value : {} ;
default: console.log(keyArr.length,key);

return newItems;