0

I have a sample json like

var obj={
    "results":{
        "grade":"A",
        "marks":12
    },
    "data":{
        "name":"sam",
        "gender":"male",
        "age":10
    }
};

Now I want to transform above json to:

var obj={
    "results":{
        "grade":"A",
        "marks":12
    },
    "name":"sam",
    "age":10
}

I was using for each loop

for(var exKey in obj) {
    if(exKey=='data'){
        //replace key data with its value
    }
}

But I was unable to figure out the logic which would do this. So can someone help me here how can I achieve this? Thanks for all ur response, But if iam having some complicated json like:

var obj={
"results":{
    "grade":"A",
     "res":"fail",
    "marks":12
},
"data":{details:{"name":"sam",
                 "gender":"male",
                  "age":10
                }
 },
 "feespaid":"yes",
 "joindate":"sunday"

};

Now if I want to modify or delete some keys inside a key value,some thing like

var obj={
"results":{
    "grade":"A",
    "marks":12
},
"data":{details:{"name":"sam",
                  "age":10
                }
 },
 "joindate":"sunday"
 };

So how we go inside the json and delete

3 Answers 3

3

Use inner for-in loop and use bracket-notation to assign variable as key of object

var obj = {
  "results": {
    "grade": "A",
    "marks": 12
  },
  "data": {
    "name": "sam",
    "gender": "male",
    "age": 10
  }
};
for (var exKey in obj) {
  if (exKey == 'data') {
    for (var d in obj.data) {
      if (d != 'gender') obj[d] = obj.data[d];
    }
    delete obj.data;
  }
}
console.log(obj);

Edit: Only to delete few keys from Object, use delete keyword

var obj = {
  "results": {
    "grade": "A",
    "res": "fail",
    "marks": 12
  },
  "data": {
    details: {
      "name": "sam",
      "gender": "male",
      "age": 10
    }
  },
  "feespaid": "yes",
  "joindate": "sunday"
};
delete obj.results.res;
delete obj.data.details.gender;
delete obj.feespaid;
console.log(obj);

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

4 Comments

It worked.How can I delete that gender key and its value der?
@HVarma — I have deleted gender in provided code using if (d != 'gender')
Thanks..If i want to delete the grade in results key,how can we do?
.Thanks Rayon.I posted the wrong json.I added a new one in the question..can u please have a look a help me out
2

One way to do this without using a loop:

obj.name = obj.data.name;
obj.age = obj.data.age;
delete obj.data;

Another way using a for-in loop:

for(item in obj.data) obj[item] = obj.data[item]; // Add items from obj.data to obj
delete obj.gender;                                // Remove gender
delete obj.data;                                  // Remove obj.data

Snippet:

var obj={
  "results":{
  "grade":"A",
  "marks":12
   },
  "data":{
   "name":"sam",
    "gender":"male",
   "age":10
  }
};

for(item in obj.data) obj[item] = obj.data[item];
delete obj.gender;
delete obj.data;

console.log(obj)

1 Comment

Thanks for the response @Spencer..I shared the wrong json der.I posted a new json below in my question.can u help me in that
1
for (var exKey in obj) {
    if(exKey=='data') {
        for (var dataKey in obj[exKey]) {
           obj[dataKey] = obj[exKey][dataKey];
        }
        delete obj[exKey];
    }
}

(I need to investigate the solution below a bit more) EDIT: Deleting nested properties could be done like this:

var invalidProperties = ['gender', 'feespaid']; //add more if needed
function recursivelyDeleteProperties(obj) {
    for (var key in obj) {
        if(invalidProperties.indexOf(key) !== -1) {
            delete obj[key];
        } else {
            recursivelyDeleteProperties(obj[key]);
        }
    }
}

2 Comments

Thanks for the response.By mistake I shared the wrong json.I posted a new json below in my question.can u help me in that
@HVarma look now, see if this fits your problem.

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.