How to get convert json array to comma separated values

I would like to convert the following json array to  comma separated values

i/p:

[{"filter":"ID","filterValues":["050886","050885","050884","050883","050882"]}]

expected o/p;
String field="ID"
String values= "050886","050885","050884","050883","050882";

I tried the below code , but  I am getting exception  while getting filter values. Any help would be appreciated.
String input ='[{"filter":"ID","filterValues":["050886","050885","050884","050883","050882"]}]';
JSONArray jsonarray = (JSONArray) new JSONTokener(input ).nextValue();
	
			for (int i = 0; i < jsonarray.length(); i++) {
					JSONObject jsonobject = jsonarray.getJSONObject(i);
					String field= jsonobject.getString("filter");
                                        String values= jsonobject.getString("filterValues");
			}

Open in new window

rajan416Asked:
Who is Participating?
 
Tomas Helgi JohannssonCommented:
HI!

Add this to your code

import org.json.CDL;

and this line

String myfiltervalues = CDL.toString( jsonobject.getJSONArray("filterValues")  );

Regards,
      Tomas Helgi
0
 
Tomas Helgi JohannssonCommented:
HI!

What exception do you get and what JSONTokener are you using ?
http://www.massapi.com/class/js/JSONTokener-1.html#Example6

And I believe this should work

String input ='[{"filter":"ID","filterValues":["050886","050885","050884","050883","050882"]}]';
JSONArray jsonarray = (JSONArray) new JSONTokener(input ).nextValue();
	
			for (int i = 0; i < jsonarray.length(); i++) {
					JSONObject jsonobject = jsonarray.getJSONObject(i);
					String field= jsonobject.getString("filter");
                                        String values= jsonobject.getJSONObject("filterValues");
			}

Open in new window


Regards,
     Tomas Helgi
0
 
rajan416Author Commented:
Hi,

The getJSONObject  throws compile time error for converting to string...

I tried jsonobject.getJSONArray("filterValues").toString(); and got  ["050886","050885","050884","050883","050882"]. How can i convert this to below format

 "050886,050885,050884,050883,050882"
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
gurpsbassiCommented:
Try
String myfiltervalues = CDL.rowToString( jsonobject.getJSONArray("filterValues")  );

Open in new window


CDL.toString() will expect your array to be an array of name value pairs (i.e. JSON objects).
CDL.rowToString() converts a array of strings.
0
 
rajan416Author Commented:
This is not working....
0
 
gurpsbassiCommented:
I'm getting the following output:

050886,050885,050884,050883,050882

what are you getting?
0
 
rajan416Author Commented:
This is perfect...Thank you guys...
0
 
gurpsbassiCommented:
Although, it won't give you the quotes around each of the filter values.
0
 
Mohd IlyasCommented:
Guyz, Dont struggle that much ,,,here is the best way I have:

    var distStateIds = [];
     var stateIds = distStateIds.map(function (val) {
            return val;
        }).join(',');
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.