3

I need to create an object from a JSON file. The Input Json file looks like this.

{ 
 "test":{
 "Record":1,
 "RecordValues":{ 
 "Address":"3330 Bay Rd.",
 "City":"Los Angeles",
 "SecondObject":{ 
 "1":"eins",
 "2":"zwei"
 }
 }
 }
}

What i have so far is this function..

var test = [];
function recFunc(obj, parent_id = null) {
 for(var i in obj) {
 if(typeof obj[i] == "object" && obj[i] !== null) {
 test.push({title: i, children: []});
 recFunc(obj[i], (test.length-1));
 }else {
 if(parent_id != null) {
 test[parent_id].children.push({title: (i + " : " + obj[i])});
 }else {
 test.push({title: (i + " : " + obj[i])});
 }
 }
 }
 return test;
}

The output object should be as follows.

[ 
 { "title":"Record : 1" },
 { 
 "title":"RecordValues",
 "children":[ 
 { "title":"Address : 3330 Bay Rd." },
 { "title":"City : Los Angeles" },
 { 
 "title":"SecondObject",
 "children":[ 
 { "title":"1 : eins" },
 { "title":"2 : zwei" }
 ]
 }
 ]
 }
]
georg
216k57 gold badges325 silver badges401 bronze badges
asked Apr 13, 2019 at 7:53
2
  • 1
    That target object looks like a very bad design. Can you explain why you need that? Commented Apr 13, 2019 at 7:57
  • @trincot the target object is needed for javascript plugin fancytree Commented Apr 13, 2019 at 8:08

3 Answers 3

1

You can use Object.entries and reduce with recursion

let obj = {"test":{"Record":1,"RecordValues":{"Address":"3330 Bay Rd.","City":"Los Angeles","SecondObject":{"1":"eins","2":"zwei"}}}}
let recursive = (obj) =>
 Object.entries(obj).reduce((op,[key,value]) => {
 let final = typeof value === 'object' ? {title:key, children:recursive(value)} 
 : {title: `${key}: ${value}`}
 return op.concat(final)
},[])
console.log(recursive(obj.test))

answered Apr 13, 2019 at 8:08
Sign up to request clarification or add additional context in comments.

Comments

1

Here's one possible approach, recursively iterating over the Object.entries:

const input = {
 "test": {
 "Record": 1,
 "RecordValues": {
 "Address": "3330 Bay Rd.",
 "City": "Los Angeles",
 "SecondObject": {
 "1": "eins",
 "2": "zwei"
 }
 }
 }
};
const makeNested = (currInput) => {
 return Object.entries(currInput).map(([key, val]) => (
 typeof val !== 'object'
 ? { title: `${key} : ${val}` }
 : {
 title: key,
 children: makeNested(val)
 }
 ))
};
console.log(makeNested(input.test));

answered Apr 13, 2019 at 8:04

1 Comment

I will try that when i come home. Thank you very much :)
1

You could use Object.entries() and recursively call the function if the nested value is an object:

(The Object(value) === value checks if the value is an object which is not null)

const obj = {test:{Record:1,RecordValues:{Address:"3330 Bay Rd.",City:"Los Angeles",SecondObject:{1:"eins",2:"zwei"}}}};
function transform(obj) {
 return Object.entries(obj).map(([key, value]) => {
 if(Object(value) === value)
 return { title: key, children: transform(value) }
 else
 return { title: `${key} ${value}` }
 })
}
console.log(transform(obj.test))

answered Apr 13, 2019 at 8:04

2 Comments

You reached here faster +1
and i was messing around using reduce :p i need to practice more about recursion for sure :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.