I have the following JSON in JS.
{
"url":".aspx",
"report_template_id":"1",
"interval_secs":"86400",
"analysis_type":"lite",
"email":"[email protected]",
"alerts":["num_domains", "med_load_time", "avg_load_time", "tot_req"]
}
I want the list of alerts to be removed and replaced with ma separated values as follows.
{
"url":".aspx",
"report_template_id":"1",
"interval_secs":"86400",
"analysis_type":"lite",
"email":"[email protected]",
"alerts":"num_domains,med_load_time,avg_load_time,tot_req"
}
I have the following JSON in JS.
{
"url":"http://example./main.aspx",
"report_template_id":"1",
"interval_secs":"86400",
"analysis_type":"lite",
"email":"[email protected]",
"alerts":["num_domains", "med_load_time", "avg_load_time", "tot_req"]
}
I want the list of alerts to be removed and replaced with ma separated values as follows.
{
"url":"http://example./main.aspx",
"report_template_id":"1",
"interval_secs":"86400",
"analysis_type":"lite",
"email":"[email protected]",
"alerts":"num_domains,med_load_time,avg_load_time,tot_req"
}
Share
Improve this question
edited Jul 26, 2016 at 11:10
Mritunjay
25.9k7 gold badges57 silver badges70 bronze badges
asked Jan 1, 2015 at 8:35
stationstation
7,14516 gold badges59 silver badges93 bronze badges
4
- 4 the output you said you need is not a valid json. Do you really want to break the json. – Dev Commented Jan 1, 2015 at 8:37
- The second JSON is the expected output, I want to remove list of alerts and simply have ma separated values. – station Commented Jan 1, 2015 at 8:37
- Is it correct to assume you made a typo and what you want is one string instead of several ma-separated strings? – JCOC611 Commented Jan 1, 2015 at 8:39
- 1 why do you want to convert a JSON array (with is supported by the JSON format) to a ma separated string? I can't think of a good reason to do it. It would be great if you step few steps back and ask the question – surui Commented Jan 1, 2015 at 9:00
1 Answer
Reset to default 9Just adding all the steps:-
1). Taking your JSON in a variable.
data = {"url":"http://example./main.aspx","report_template_id":"1","interval_secs":"86400","analysis_type":"lite","email":"[email protected]","alerts":["num_domains","med_load_time","avg_load_time","tot_req"]};
2). Parse JSON data to object. Assuming the JSON is a string initially do a typeof(data)
to be clear.
data = JSON.parse(data);
3) Change list of alerts to ma separated values
data.alerts = data.alerts.join(',');
4) Convert back to string
data = JSON.stringify(data)
So data will look like
{
"url": "http://example./main.aspx",
"report_template_id": "1",
"interval_secs": "86400",
"analysis_type": "lite",
"email": "[email protected]",
"alerts": "num_domains,med_load_time,avg_load_time,tot_req"
}
Note:- If you will just say join()
then also it will work, because default delimiter is ,
only, just to clarify I have given that.