2

I have the following JSON in JS.

{
   "url":"http://example.com/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 comma separated values as follows.

{
   "url":"http://example.com/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"
}
4
  • 4
    the output you said you need is not a valid json. Do you really want to break the json. Commented Jan 1, 2015 at 8:37
  • The second JSON is the expected output, I want to remove list of alerts and simply have comma separated values. 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 comma-separated strings? 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 comma 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 Commented Jan 1, 2015 at 9:00

1 Answer 1

9

Just adding all the steps:-

1). Taking your JSON in a variable.

data = {"url":"http://example.com/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 comma separated values

data.alerts = data.alerts.join(',');

4) Convert back to string

data = JSON.stringify(data)

So data will look like

{
    "url": "http://example.com/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.

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

9 Comments

data.alerts.concat() would also do the job.
This throws an error. Uncaught TypeError: Cannot read property 'join' of undefined
@user567797 data here is just whatever the name of your variable that holds the json is. Make sure you use the right varriable name.
If I do an alert(data.alerts); it gives me undefined whereas an alert(data); gives me the whole json. Somejhow the querying is wrong I guess
@user567797 just do a typeof(data) and if it says string do a data=JSON.parse(data) it will convert data to object from string.
|

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.