0

I have the following JSON file:

{
  "meta" : {
    "stock" : "AWS",
    "date modified" : 90
  },
  "roles" : [ "Member", "Admin" ],
  "name" : "John Doe",
  "admin" : true,
  "email" : "[email protected]"
}

I wanted to both read the values of the keys and add them to an Array List.

try {
    // create object mapper instance
    ObjectMapper mapper = new ObjectMapper();

    // convert JSON file to map
    Map<?, ?> map = mapper.readValue(Paths.get("user.json").toFile(), Map.class);

    ArrayList<String> data = new ArrayList<String>();
    
    // print map entries
    for (Map.Entry<?, ?> entry : map.entrySet()) {
        System.out.println((entry.getClass()) + "  " + entry.getValue());
        data.add((String)entry.getValue()); // trying to add entry values to arraylist
    }
} catch (Exception ex) {
    ex.printStackTrace();
}

I'm able to print out the data type of the value along with the value itself. All the values are part of class java.util.LinkedHashMap$Entry. I'm not able to cast the values to a String to add them to an ArrayList. How should I go about doing this? Thanks

5
  • // convert JSON file to map - from the json, please tell what the key is and what part is the value? Commented Oct 21, 2021 at 0:55
  • @ScaryWombat keys: "meta", "roles", "admin", "email" values: object of "stock" and "date modified", list containing "member" and "admin", "John Doe", true, and "[email protected]" Commented Oct 21, 2021 at 0:58
  • What String value are you expecting to get for "meta" or "roles"? Commented Oct 21, 2021 at 1:09
  • @tgdavies {stock=AWS, date modified=90} and [Member, Admin] respectively Commented Oct 21, 2021 at 1:14
  • Then you'll need to render your values to Strings, casting doesn't convert non-Strings to Strings Commented Oct 21, 2021 at 1:18

2 Answers 2

1

From the jackson-databind documentation you can convert your json to a Map<String, Object> map with the following line (you have boolean, list, number and string values in your json) :

Map<String, Object> map = mapper.readValue(json, Map.class);
// it prints {meta={stock=AWS, date modified=90}, roles=[Member, Admin], name=John Doe, admin=true, [email protected]}
System.out.println(map);

If you want to save your map values string representation into an ArrayList data you can iterate over them with a loop :

List<String> data = new ArrayList<>();
for (Object value : map.values()) {
    data.add(value.toString());
}
//it will print [{stock=AWS, date modified=90}, [Member, Admin], John Doe, true, [email protected]]
System.out.println(data);
Sign up to request clarification or add additional context in comments.

Comments

1

Your data type of entries will be like:

meta: Map<String:Object>
roles: List<String>
admin: Boolean

So you will get an exception when casting to string for each entry value. You should handle different data type and convert it according to your request:

Object value = entry.getValue();

I highly recommend you write more few functions to check and convert map/list/primitive variables to expected data (String):

boolean isList(Object obj);
boolean isMap(Object obj);
...
public List<String> convertMap(Map<String,Object> map);
...

Comments

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.