4

I have a json file with same key but different values as follows,

{
    "domains" : {
        "A" : {
            "name" : "a",
            "type" : "a1"
        },
        "B"  :{
            "name" : "r",
            "type" : "g1"
         },
        "A" : {
           "name" : "b",
           "type" : "b1"
        }
    }
}

which is coming from external system. How to convert the json to java map object and access the different values of the key: A

I am using something like below,

map = mapper.readValue(json, new TypeReference<HashMap<String,String>>(){});

which returns a map with unique keys. But I need a map object to hold all the data from json file.

Anyway to achieve this?

Nick
  • 1,417
  • 1
  • 14
  • 21
user1321824
  • 445
  • 3
  • 22
  • 41
  • 11
    The results of parsing such JSON are undefined "by definition"; the only way you'll be able to do that with Jackson is to parse it yourself (instantiate a `JsonParser`; and read token by token). First things first however, you should whack the producer of that JSON with a sledgehammer and tell him/her to go read RFC 7159. – fge Dec 30 '14 at 18:23
  • 4
    By the way, the relevant text: [RFC 7159, section 4](https://tools.ietf.org/html/rfc7159#section-4): "When the names within an object are not unique, the behavior of software that receives such an object is unpredictable." – fge Dec 30 '14 at 18:47

2 Answers2

2

I agree with comments by @fge.

But if you really insists on solving this, you could sub-class HashMap (or any other Map), override its put method, and handle duplicates using whatever mechanism you want. Just make sure your Map has a no-arguments constructor.

Guava may also have a datatype that would allow retaining duplicates (Multimap?). If so, you will want to use Jackson's Guava module: https://github.com/FasterXML/jackson-datatype-guava

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • 3
    Actually, with Guava there is an even more simple way: use a `ForwardingMap`. Well, that doesn't prevent the fact that the JSON is broken to start with, of course. – fge Dec 30 '14 at 18:36
1

Answering since a related question was marked duplicate but seems to address the reverse problem where JSON with duplicate keys needs to be created. So that is Java Model -> JSON with duplicate keys.

Simplest way I found was to implement a custom serializer.

public class CustomSerializer extends StdSerializer<ModelToSerialize> {


    public CustomSerializer(Class<ModelToSerialize> t) {
        super(t);
    }

    @Override
    public void serialize(ModelToSerialize value, com.fasterxml.jackson.core.JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        gen.writeStringField("DuplicateJsonKey", value.getDup1());
        gen.writeStringField("DuplicateJsonKey", value.getDup2());
        // Map all other values on value that you want to be present in the JSON
        gen.writeEndObject();
    }
}

And then when you go to serialize:

SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(ModelToSerialize.class, new CustomSerializer(ModelToSerialize.class));
objectMapper.registerModule(simpleModule);
String sr = objectMapper.writeValueAsString(modelToSerialize);
BillD
  • 23
  • 5