user2900150
user2900150

Reputation: 441

How can I change this json structure?

I have a json response which I have got after

result = JSON.parse(result.value);

json response

"{"name":"For ","children":["{ \"name\":\"sxsm cnklsd\"}","{ \"name\":\"smd csdm\"}"]}".

Now I am trying to convert this in to a structure as:

{
    "name": "For  ",
    "children": [
        {
            "name": "sxsm cnklsd"
        },
        {
            "name": "smd csdm"
        }
    ]
}

I tried to double parsing , stringify and then parse but nothing seems to be working. Please help.

Upvotes: 0

Views: 70

Answers (1)

user663031
user663031

Reputation:

Parse the JSON as you have done:

> result = JSON.parse(result.value);
< {"name":"For ","children":["{ \"name\":\"sxsm cnklsd\"}","{ \"name\":\"smd csdm\"}"]}
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^

children is an array containing two JSON strings. Parse them:

> result.children = result.children.map(JSON.parse)
> result
< {"name":"For ", "children":[{ "name":"sxsm cnklsd"}, {"name":"smd csdm"}]}

Upvotes: 1

Related Questions