I need to parse a JSON object recursively.
Here is a sample JSON:
const obj = {
tag: 'AA',
type: 'constructed',
value: 'ABCD1',
child: [
{
tag: 'BB',
type: 'constructed',
value: 'ABCD2',
child: [
{
tag: 'CC',
type: 'constructed',
value: 'ABCD3',
child: [
{
tag: 'DD',
type: 'primitive',
value: 'ABCD4',
child: []
},
{
tag: 'EE',
type: 'constructed',
value: 'ABCD5',
child: [
{
tag: 'FF',
type: 'primitive',
value: 'ABCD6',
child: []
},
{
tag: 'GG',
type: 'primitive',
value: 'ABCD7',
child: []
}
]
},
{
tag: 'HH',
type: 'primitive',
value: 'ABCD8',
child: []
}
]
}
]
},
{
tag: 'II',
type: 'primitive',
value: 'ABCD9',
child: []
}
]
}
And the output should be something like:
{
"AA": [
{
"BB": [
{
"CC": [
{
"DD": "ABCD4"
},
{
"EE": [
{
"FF": "ABCD6"
},
{
"GG": "ABCD7"
}
]
},
{
"HH": "ABCD8"
}
]
}
]
},
{
"II": "ABCD9"
}
]
}
Basically, those with type constructed
should have a nested object and those that are primitive
should directly have a key-value pair. The object could go very deep, and can have both constructed and primitive objects on the same level (as on my example).
Here is my current code:
let jsonOutput = {}
parseData(obj, jsonOutput)
function parseData(jsonToParse, jsonOutput) {
const type = jsonToParse.type
const tag = jsonToParse.tag
const value = jsonToParse.value
let prev = jsonOutput
if (type === 'constructed') {
prev[tag] = []
return parseData(jsonToParse.child[0], prev[tag])
} else if (type === 'primitive') {
prev[tag] = value
return parseData(jsonToParse.child, prev[tag])
}
}
Here is my fiddle:
https://jsfiddle.net/kzaiwo/0v6a2tp8/16/
But I can’t make it iterate through the whole object. Recursions are not really my strongest area, but I think this is the most optimal way to implement it.. What am I missing? Help please!
Thanks!
2
Answers
You can use the following recursive
reduceObj(obj)
function, short and sweet:Function syntax prior to ES6:
Hope this may help you,