1

com.amazonaws.util.json.JSONObject Below is the List, i want to convert it into json string.

List<JSONObject> jsonObjlist is

[{"Attribute":"EmailAddress","Value":"[email protected]"}, {"Attribute":"Source","Value":"Missing_Fixed"}, {"Attribute":"mx_Lead_Status","Value":"Registered User"}, {"Attribute":"mx_Age","Value":""}, {"Attribute":"mx_LoginID","Value":"[email protected]"}, {"Attribute":"mx_Registration_Source","Value":"EMAIL"}]
ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        String arrayToJson = objectMapper.writeValueAsString(jsonObjlist);

Output i get is

[{"map":{"Attribute":"EmailAddress","Value":"[email protected]"}},{"map":{"Attribute":"Source","Value":"Missing_Fixed"}},{"map":{"Attribute":"mx_Lead_Status","Value":"Registered User"}},{"map":{"Attribute":"mx_Age","Value":""}},{"map":{"Attribute":"mx_LoginID","Value":"[email protected]"}},{"map":{"Attribute":"mx_Registration_Source","Value":"EMAIL"}}]

Desired out put is

"[{"Attribute":"EmailAddress","Value":"[email protected]"}, {"Attribute":"Source","Value":"Missing_Fixed"}, {"Attribute":"mx_Lead_Status","Value":"Registered User"}, {"Attribute":"mx_Age","Value":""}, {"Attribute":"mx_LoginID","Value":"[email protected]"}, {"Attribute":"mx_Registration_Source","Value":"EMAIL"}]"
3
  • You should also add your desired output, not just the one you get now. Commented Apr 4, 2018 at 11:57
  • If you are using Amazon AWS's JSONObject, you should also include relevant import statements, as most readers (including me) will otherwise assume that you are using org.json classes. Commented Apr 4, 2018 at 12:25
  • Yes using com.amazonaws.util.json.JSONObject Commented Apr 4, 2018 at 12:38

2 Answers 2

6

You should convert your list to a JSON Array, and just use its toString() function:

JSONArray myArray = new JSONArray(jsonObjlist);

// ...
String arrayToJson = myArray.toString(2);

The int parameter specifies the indent factor to use for formatting.

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

2 Comments

I am getting this {"myArrayList":[{"map":{"Attribute":"EmailAddress","Value":"[email protected]"}},{"map":{"Attribute":"Source","Value":"Missing_Fixed"}},{"map":{"Attribute":"mx_Lead_Status","Value":"Registered User"}},{"map":{"Attribute":"mx_Age","Value":""}},{"map":{"Attribute":"mx_LoginID","Value":"[email protected]"}},{"map":{"Attribute":"mx_Registration_Source","Value":"EMAIL"}}]}
@VinaySawant Right, ObjectMapper is for Java -> JSON objects. You can just use toString(), changed my answer.
-1

You can also direct use

 String jsonString = new ObjectMapper().writeValueAsString(jsonObjectList) 

To get the desired ouptput

Here is the small example

List<JSONObject> jsons = new ArrayList<>();
jsons.add(new JSONObject(ImmutableMap.of("Attribute", "EmailAddress", "Value", "[email protected]")));
jsons.add(new JSONObject(ImmutableMap.of("Attribute1", "EmailAddress3", "Value1", "[email protected]")));
System.out.println(new ObjectMapper().writeValueAsString(jsons));

The output is

[{"Value":"[email protected]","Attribute":"EmailAddress"},{"Attribute1":"EmailAddress3","Value1":"[email protected]"}]

I suspect there are some toString() method in any object that you have override?

5 Comments

Isn't this (almost) exactly what the OP tries, and what does not have desired effect?
com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.amazonaws.util.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.ArrayList[0])
com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.amazonaws.util.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.ArrayList[0])
Did you check if there are any toString method in your bean?
Just try out what you have tried in the question, but only this time without the toString method. I assume I did not use any kind of bean that's why I cannot reproduce it.

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.