convert stringified array of objects to json object | Community
Skip to main content
July 21, 2023
Solved

convert stringified array of objects to json object

  • July 21, 2023
  • 5 replies
  • 9340 views

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.

Best answer by Rohan_Garg

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! 

5 replies

Community Advisor
July 21, 2023

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


aem_noobAuthor
July 25, 2023

i tried into jsonarray too but that didn't work

Nilesh_Mali
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

aem_noobAuthor
July 25, 2023

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

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

 

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

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

aem_noobAuthor
July 25, 2023

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

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! 

aem_noobAuthor
July 25, 2023

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