0

I am getting back data in a format that is not acceptable to the processing system and I am trying to convert the data. Below is the data I get and the required JSON below that. I tried different things like finding an Object within the data and checking if it has more than one element then converting that object to Array[] but I am unable to do so.

If you have any inputs, I would appreciate it.

if(typeof ob1=== "object" && Object.keys(ob1.length > 1) && typeof Object.keys(ob1) === "object" )
{
    console.log(ob1); // I get all the objects and not the parent object i need to change. 
}

Present data:

ob1 : {id: 1, details: Object, profession: "Business"}

JSON:

{
  "id": "1",
  "details": {
    "0": {
      "name": "Peter",
      "address": "Arizona",
      "phone": 9900998899
    },
    "1": {
      "name": "Jam",
      "address": "Kentucky",
      "phone": 56034033343
    }
  },
  "profession": "Business"
}

Required data:

{id: 1, details: Array[2], profession: "Business"}

Required JSON:

{
  "id": "1",
  "details": [
    {
      "name": "Peter",
      "address": "Arizona",
      "phone": 9900998899
    },
    {
      "name": "Jam",
      "address": "Kentucky",
      "phone": 56034033343
    }
  ],
  "profession": "Business"
}
1
  • 2
    JSON is a string/text format. So there's no such thing as a "JSON object" or "JSON array". Commented Mar 2, 2017 at 20:54

1 Answer 1

2

You have to go through the details object and convert it into an array:

var x = {
  details: {

    0: {a: 1},
    1: {a: 2}
  }
}

var detailsArr = [];

for(key in x.details) {
  detailsArr.push(x.details[key]);
}

x.details = detailsArr;

//x.details = [{a: 1}, {a: 2}]
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.