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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.