0

I have a file of JSON-serialized data that looks like this:

{
    "name":"Store",
    "children":[
        {
            "name":"Store 1",
            "children":[
                {
                    "name":"collection 1",
                    "description":"collection 1",
                    "children":[
                        {
                            "name":"Products",
                            "description":"Products",
                            "children":[
                                {
                                    "name":"Product 1",
                                    "description":"Product 1"
                                }
                            ]
                        }
                    ]
                }
            ],
            "description":"category 1"
        },
        {
            "name":"Store 2"
        }
    ]
}

For objects with a name property, I want to add a title whose value is the same as the value of the name property. Below is what I am trying to convert my JSON to:

{
    "name":"Store",
    "title":"Store",
    "children":[
        {
            "name":"Store 1",
            "title":"Store 1",
            "children":[
                {
                    "name":"collection 1",
                    "title":"collection 1",
                    "description":"collection 1",
                    "children":[
                        {
                            "name":"Products",
                            "title":"Products",
                            "description":"Products",
                            "children":[
                                {
                                    "name":"Product 1",
                                    "title":"Product 1",
                                    "description":"Product 1"
                                }
                            ]
                        }
                    ]
                }
            ],
            "description":"category 1"
        },
        {
            "name":"Store 2",
            "title":"Store 2"
        }
    ]
}
4
  • What code have you tried? Show us that, and maybe we can tell you where you're going wrong. Commented Oct 17, 2019 at 4:14
  • the farthest i could reach is replacing it by JSON.stringify(data).replace(/"name"/g, '"title"'), but i cant find a method or resources on how to duplicate the object. Commented Oct 17, 2019 at 4:17
  • 1) you said the daya you have already IS JSON (in a file), so why would you stringify it? 2) never manipulate your data via string operations on the serialized form Commented Oct 17, 2019 at 4:24
  • so what should i do in this case? .map it and restructure it again? Commented Oct 17, 2019 at 4:27

2 Answers 2

2

we can use parse the Json using JSON.Parse and use recursion to add title as below to all the children

function Recursion(items) {
  items["title"] = items["name"]
  if (items["children"] != undefined) {
    items["children"].forEach(element => {
      element = Recursion(element)
    });
  }
  return items
}



 var text = '{"name":"Store","children":[{"name":"Store 1","children":[{"name":"collection 1","description":"collection 1","children":[{"name":"Products","description":"Products","children":[{"name":"Product 1","description":"Product 1"}]}]}],"description":"category 1"},{"name":"Store 2"}]}';
  var item = JSON.parse(text);
  item = Recursion(item);
Sign up to request clarification or add additional context in comments.

1 Comment

+1 In my own fiddling around I used map to do the recursion but it all works out the same transform = function (d) { if (d.name) { d.title = d.name; } if (d.children) { d.children = d.children.map(transform); } return d; }
0

const addTitleRec = (j) => {
  j.title = j.name, j.children && j.children.forEach(addTitleRec);
}

const json = {
  "name": "Store",
  "children": [{
      "name": "Store 1",
      "children": [{
        "name": "collection 1",
        "description": "collection 1",
        "children": [{
          "name": "Products",
          "description": "Products",
          "children": [{
            "name": "Product 1",
            "description": "Product 1"
          }]
        }]
      }],
      "description": "category 1"
    },
    {
      "name": "Store 2"
    }
  ]
};

addTitleRec(json);

console.log(json);

Comments

Your Answer

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.