0

I have the JSON like

var resultJSON = `{
  "data": {
    "total": 1,
    "list_name": "title",
    "title": {
      "id": 53,
      "name": "Sonu",
      "mobileNo": "6543213456",
      "address": "Greeny Pathway",
      "city": "NewYork",
      "mode": "Weekly",
      "duration": "15",
      "qty": null
    },
    "download": [{
      "time": "16789042",
      "date": "26 - 01 - 2020"
    }]
  }
}`;

I expect the output:

{
  "total": "1",
  "list_name": "title",
  "name": "sonu",
  "mobileno": "6543213456"
}

Here "list_name": "title" is dynamic, sometimes it will come "list_name": "book", based on that above mentioned response I want to get.

1
  • Consider JSON.parse? (That's actual JSON for once) Commented Jan 27, 2020 at 12:41

1 Answer 1

1

Something like this? I had to fix your invalid JSON

You can make it more clever if you study https://javascript.info/destructuring-assignment in depth

const resultJSON = `{
  "data": {
    "total": 1,
    "list_name": "title",
    "title": {
      "id": 53,
      "name": "Sonu",
      "mobileNo": "6543213456",
      "address": "Greeny Pathway",
      "city": "NewYork",
      "mode": "Weekly",
      "duration": "15",
      "qty": null
    },
    "download": [{
      "time": "16789042",
      "date": "26-01-2020"
    }]
  }
}`
const data = JSON.parse(resultJSON).data
const content = data[data.list_name];
let newObj = {} 

newObj["total"]     = data["total"];
newObj["list_name"] = data["list_name"];
newObj["name"]      = content["name"];
newObj["mobileNo"]  = content["mobileNo"];
console.log(newObj)

Sign up to request clarification or add additional context in comments.

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.