Skip to main content
Level 4
July 21, 2023
Solved

convert stringified array of objects to json object

hi this is more of a java based question but I could use some help here-

 

i have a stringified (json.stringify) javascript array of strings ["abc","efg","ijk"]

i want to convert it to a json object

 

JsonObject jsonObject = new Gson().fromJson(arrayOfStrings, JsonObject.class);
com.google.gson.JsonSyntaxException: Expected a com.google.gson.JsonObject but was com.google.gson.JsonArray

 

i have tried using JsonParser from gson, javax.json.stream but nothing seems to work.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Rohan_Garg
Rohan_GargAccepted solution
Community Advisor
July 25, 2023

If it is only an array of string you want, you can use the below - 

List<String> currentGroupIdsList = new ArrayList<String>(Arrays.asList(stringifiedArray.split(",")));

Here stringifiedArray is received from the frontend and converted into simple <List> of strings.

Hope this helps! 

Rohan Garg

10 replies

ShubhanshuSi2
Community Advisor
Community Advisor
July 21, 2023

Try .fromJson(arrayOfStrings, JsonArray,class)  instead of converting it into JsonObject.class


Translate
Shubhanshu Singh | Technical Architect | AEM Assets & Sites
aem_noobAuthor
Level 4
July 25, 2023

i tried into jsonarray too but that didn't work

Translate
Nilesh_Mali
Level 3
July 21, 2023

To fix this, you should use the appropriate data structure for deserializing the JSON array. In this case, you should use JsonArray instead of JsonObject since the input is an array of strings.

 

 

String arrayOfStrings = "[\"abc\",\"efg\",\"ijk\"]"; // Parse the JSON array into a JsonArray JsonArray jsonArray = new Gson().fromJson(arrayOfStrings, JsonArray.class); // Now you can work with the JsonArray as needed sout(jsonArray); // Output: ["abc","efg","ijk"]

 

 

JsonArray object is ready here to access the individual elements within the array if needed

Translate
aem_noobAuthor
Level 4
July 25, 2023

I have tried json array too but that throws an error too.

Translate
nitesh_kumar-1
Adobe Employee
Adobe Employee
July 21, 2023

Hi @aem_noob ,

 

I don't know what a full string looks like, but this seems to be JsonArray, try using that class instead.

 

JsonArray jsonArray =  new Gson().toJsonTree(arrayOfStrings).getAsJsonArray();

You could then iterate and read the JsonObject in this array, or form the proper JsonString which would qualify as JsonObject.

Also, check https://www.tutorialspoint.com/how-to-convert-java-array-or-arraylist-to-jsonarray-using-gson-in-java 

 

Hope this helps.

 

Regards,

Nitesh

 

Translate
aem_noobAuthor
Level 4
July 25, 2023

i tried jsonarray initially but that also threw an exception. the response from front end is a json of array objects which is then filtered to array of strings. the array of strings is not getting cast to jsonarray.

Translate
ksh_ingole7
Community Advisor
Community Advisor
July 21, 2023

Hi @aem_noob 

 

Try this ->

 

const array = ["abc", "efg", "ijk"];

 

// Convert array to JSON object const jsonObject = JSON.stringify(array);

 

console.log(jsonObject);

Translate
aem_noobAuthor
Level 4
July 25, 2023

The array of strings is not getting cast to jsonObject as I have tried it

Translate
Rohan_Garg
Community Advisor
Rohan_GargCommunity AdvisorAccepted solution
Community Advisor
July 25, 2023

If it is only an array of string you want, you can use the below - 

List<String> currentGroupIdsList = new ArrayList<String>(Arrays.asList(stringifiedArray.split(",")));

Here stringifiedArray is received from the frontend and converted into simple <List> of strings.

Hope this helps! 

Translate
Rohan Garg
aem_noobAuthor
Level 4
July 25, 2023

this works but isn't it possible to do it via jsonArray or jsonObject?

Translate