JSON representation conversion
I am a beginner at javascript, I am trying to generate an icicle graph using my JSON. The form my JSON is in is similar to the example seen at the bottom of the page here
I want my JSON to be represented the way it is at the bottom of the example here
So that I can use that icicle graph instead. Is there a way to accomplish this task without doing it manually?
I want to use this javascript code, which will only work with the second JSON format/representation:
Since your data is pretty simple — just name, size, and children, and size/children are mutually exclusive, you can make a simple function that creates an object based on name and size an then recursively call that on the children using Object.assign to help.
The way this works: convertObject takes an object and returns an object with a name property. If the obj passed in has a size the returned object will just be {obj.name: obj.size}. If the passed in object has a children property, it will call convertObject with each child. Each of those calls will return an object and reduce will merge them all into one object, which we call ch above. In the cases where there are children the converObject function returns an object in the form {object.name, ch}.
It's understandable that this can be a little hard to wrap your head around — recursive functions take a little getting used to.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.