4

Note: Before posting this here, I spent a chunk of time trying to look for a solution about this here, but couldn't find any.

I have this JSON file that contains multiple json objects that I want to read into a map, each object is of the format {"key1":"value1", "key2":"value2"}, I tried

Map<String, String> map = MAPPER.readValue(fixture("fixtures/file_name.json"), new TypeReference<Map<String, String>>() {});

as that worked for reading a file into a map of map of list (Map<String, Map<String, List<String>>>). Is there something that should be different?

My .json file looks like

[
   {"key1":"value1_1", "key2":"value2_1"},
   {"key1":"value1_2", "key2":"value2_2"},
   ...
]

I have also another json file that I want to read into a map, that looks like

[
   "key1": {
       "key1_1": [
           {"key1_1_1":"value1", "key1_1_2":"value2"},
           ...
       ],
       ...
    },
    "key2":<int>,
    "key3":{
        "key3_1":[],
        ...
    },
    ...
    "key_n":<string>,
    ...
]

Can you help me map those files correctly, without writing too many lines of code?

2 Answers 2

1

As you are handling different cases of nested objects varying from Strings to JsonArrays it would be good to implement your map as Map<String, Object>.

Follow the below implementation,

ObjectMapper mapper = new ObjectMapper();
MapType type = mapper.getTypeFactory().constructMapType(
    Map.class, String.class, Object.class);
Map<String, Object> data = mapper.readValue(jsonString, type);

Here I am reading the JSON as a string. You can change your implementation to accommodate file reading as well.

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

3 Comments

these classes you are using to create the mapper are in which import? I am using the Jackson import, but I am getting this error com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [collection type; class java.util.List, contains [simple type, class java.lang.Object]]
These are the regular java imports. You might be doing something else wrong as there is no list implementation in my code here but you are trying to map from list. Can you share your code ?
Sorry @Coder, it's my mistake. There was a part I didn't remove. All is working fine now. Thank you.
0

I would suggest you to use the Jackson library.

Here is a mkyong tutorial

Just change the code to

Map map = mapper.readValue(new File("c:\\whatever.json"), Map.class);

7 Comments

Why are you using Windows paths in your answer?
@m0skit0 I guess, he did it, because mkyong did it in his tutorial
@m0skit0 I just changed the code in the tutorial to convert the json object to a map. The path can be any path. BTW, I am not sure why my answer got down voted.....
Because you're using Windows paths which are really not standard and not even used in the question. I would actually remove it but I can't unless you edit your question. While you're at it, you can remove Windows paths.
@m0skit0 As I stated, this is just a copy of the tutorial, the reason why I left the path there is that I just want to make it more clear, which line I was modifying. To be honest, I don't think any dev would be confused by a windows path, since this is just a path and can be changed to anything.
|

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.