0

How would I edit a JSON array after it's been encoded as JSON?

I would like to edit the following JSON array :

{"Status":"OK","Message":"API Call worked","Result":[{"interval":"2014-11-30",
"leads":0,"name":"CarEnquiry","status":"NEW","appointment":0},
{"interval":"2014-11-30","leads":0,"name":"CarEnquiry","status":"CALL1","appointment":0},
{"interval":"2014-11-30","leads":0,"name":"CarEnquiry","status":"CALL2","appointment":0}]}

To change it to this

{"Result":"OK","Records":[{"interval":"2014-11-30",
"leads":0,"name":"CarEnquiry","status":"NEW","appointment":0},
{"interval":"2014-11-30","leads":0,"name":"CarEnquiry","status":"CALL1","appointment":0},
{"interval":"2014-11-30","leads":0,"name":"CarEnquiry","status":"CALL2","appointment":0}]}

How would I edit the array in Javascript?

aphextwix
  • 1,838
  • 3
  • 21
  • 27
  • May I ask why you're needing to do this; why you're unable to do this when the original array is being generated; and how this JSON is generated in the first place? – James Donnelly Dec 12 '14 at 11:03
  • Describe what are the differences. – Ionică Bizău Dec 12 '14 at 11:03
  • 1
    Maybe just 30 mn training on manipulating javascript objects would help (it is easy). –  Dec 12 '14 at 11:06
  • @KavehRassoulzadegan - I did a Google around, couldn't find anything relevant. – aphextwix Dec 12 '14 at 11:07
  • @JamesDonnelly - the encoding of arrays is done by an overall API controller which generates the same format for all JSON objects - i.e. `"Status":"OK","Message":"API Call worked","Result":[`. I'm using a third party library that expects the beginning line to be `{"Result":"OK","Records":[`. So just doing a workaround. – aphextwix Dec 12 '14 at 11:08
  • @aphextwix that's a bit strange, because usually in calls like this "Status" is used to refer to the status of the call (e.g. OK) and "Result" is used to refer to the actual result of the call. This is exactly what your first JSON object does. – James Donnelly Dec 12 '14 at 11:09
  • @JamesDonnelly - strange indeed, but that's what jTable states in the documentation. [link](http://jtable.org/GettingStarted) – aphextwix Dec 12 '14 at 11:11
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects –  Dec 12 '14 at 11:18

3 Answers3

1

If you really want to do this with JavaScript you can simply create a new JavaScript object (which I've called newJSON below), and manually assign the properties of that as properties of your current JSON (which I've called oldJSON) to it:

var oldJSON = ...,
    newJSON = {};

newJSON.Result = oldJSON.Status;
newJSON.Records = oldJSON.Result;

newJSON is now a JavaScript object in the format you desire. If we call JSON.stringify on this, we'll see the result. Click Run code snippet on the below snippet to have the stringified result alerted to you.

var oldJSON = {"Status":"OK","Message":"API Call worked","Result":[{"interval":"2014-11-30","leads":0,"name":"CarEnquiry","status":"NEW","appointment":0},{"interval":"2014-11-30","leads":0,"name":"CarEnquiry","status":"CALL1","appointment":0},{"interval":"2014-11-30","leads":0,"name":"CarEnquiry","status":"CALL2","appointment":0}]},
    newJSON = {};

newJSON.Result = oldJSON.Status;
newJSON.Records = oldJSON.Result;

alert(JSON.stringify(newJSON));
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
0
x['Records']=x.Result;
x['Result']=x.Status;

if you don't mind some extra keys in the json then you dont need to remove them otherwise if you want to remove them then check these questions here. Hope it helps.

Community
  • 1
  • 1
Aameer
  • 1,366
  • 1
  • 11
  • 30
0

Maybe you can try modify and/or remove json keys.

I would do something like this:

var json = '
    {"Status":"OK",
     "Message":"API Call worked",
     "Result": [
        {"interval":"2014-11-30", "leads":0,"name":"CarEnquiry","status":"NEW","appointment":0},
        {"interval":"2014-11-30", "leads":0,"name":"CarEnquiry","status":"CALL1","appointment":0},
        {"interval":"2014-11-30", "leads":0,"name":"CarEnquiry","status":"CALL2","appointment":0}]}
';

var obj = JSON.parse(json);

obj.Records = obj.Result;
delete obj.Result;

obj.Result = obj.Status;
delete obj.Status;

delete obj.Message;

json = JSON.stringify([obj]);

You would obtain:

{"Records": [
     {"interval":"2014-11-30", "leads":0,"name":"CarEnquiry","status":"NEW","appointment":0},
     {"interval":"2014-11-30", "leads":0,"name":"CarEnquiry","status":"CALL1","appointment":0},
     {"interval":"2014-11-30", "leads":0,"name":"CarEnquiry","status":"CALL2","appointment":0}
   ],
 "Result":"OK"
}
pidupuis
  • 343
  • 6
  • 15