0

I have JSON literal as given below.

{
  "GB":[
        {"name":"Bob","score":9},
        {"name":"Joe","score":6},
        {"name":"Tom","score":7}
  ],
  "GP":[
        {"name":"Mahesh","score":19},
        {"name":"Anand","score":62},
        {"name":"Sawapnil","score":76}
  ]
}

How can edit or remove from above JSON. Like, If GB -> Score: 7 to Score: 73 and remove GP -> {"name":"Anand","score":62}

Rohan Jain
  • 27
  • 7
  • Are you asking how to load this JSON text from a file into a structure in JavaScript and modify that structure? JSON is a text format, so you need to 1) parse the text into a data structure, 2) modify the data structure, and 3) re-serialize the modified structure back into JSON text. It's not clear which of these steps to want to do and/or need help with. – apsillers Oct 03 '16 at 17:31
  • Question is not clear :( – Pranesh Ravi Oct 03 '16 at 17:31
  • There isn't really something like a JSON "literal". You either have JSON (a data exchange format), or you have JavaScript object literal. – Felix Kling Oct 03 '16 at 17:34
  • 1
    If this is just about processing JavaScript objects / arrays, it's probably a duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196) and [How to remove a particular element from an array in JavaScript?](http://stackoverflow.com/q/5767325/218196) – Felix Kling Oct 03 '16 at 17:38

1 Answers1

1

You turn it into an array and then back to json.

var arr = $.map(obj, function(el) { return el });

arr['GB'][3]['score'] = 73;

var myJsonString = JSON.stringify(arr);

or as a javascript object:

var jsonobj = JSON.parse(json);

jsonobj.GB[3].score = 73;

var myJsonString = JSON.stringify(arr);
Aschab
  • 1,378
  • 2
  • 14
  • 31