Reputation: 47
i am trying to put json data into HashMap which may contain lot of duplicate keys. For example:
{
"member Detial": {
"firstname": "jhon",
"lastName": "wick",
"Address": "New York",
"Email": "[email protected]",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"Password": "newyorkcitizen",
"confirm password": "newyorkcitizen",
"NationalProviderIdentifier": "Apollo",
"PartyID": "1"
},
"Physician": {
"firstname": "jhon",
"lastName": "wick",
"Email": "[email protected]",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"NationalProviderIdentifier": "Apollo",
"PartyID": "1"
},
"PhysicianGroup": {
"Address": "New York",
"Email": "[email protected]",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"NationalProviderIdentifier": "Apollo",
"Hospital name": "Apollo",
"Tax ID": "12345",
"PartyID": "1"
},
"Physician": {
"Address": "New York",
"Email": "[email protected]",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"NationalProviderIdentifier": "Apollo",
"Hospital name": "Apollo",
"Tax ID": "12345",
"PartyID": "1"
},
}
in the above json file "member details" is considered as key and data within brackets is considered as value. but problem here is there can be duplicate keys like "Physician" in above json example.
i want to add value of second "physician" to first "Physician" without overriding its value and delete the duplicate key. for example:`
{
"member Detial": {
"firstname": "jhon",
"lastName": "wick",
"Address": "New York",
"Email": "[email protected]",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"Password": "newyorkcitizen",
"confirm password": "newyorkcitizen",
"NationalProviderIdentifier": "Apollo",
"PartyID": "1"
},
"Physician": [{
"firstname": "jhon",
"lastName": "wick",
"Email": "[email protected]",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"NationalProviderIdentifier": "Apollo",
"PartyID": "1"
},
{
"Address": "New York",
"Email": "[email protected]",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"NationalProviderIdentifier": "Apollo",
"Hospital name": "Apollo",
"Tax ID": "12345",
"PartyID": "1"
}
],
"PhycianGroup": {
"Address": "New York",
"Email": "[email protected]",
"Fax Number": "09876543",
"Contact Number": "9876543210",
"NationalProviderIdentifier": "Apollo",
"Hospital name": "Apollo",
"Tax ID": "12345",
"PartyID": "1"
}
}
`
Upvotes: 0
Views: 165
Reputation: 131147
Guava comes with Multimap<K, V>
. This is exactly what you are looking for:
A collection that maps keys to values, similar to
Map
, but in which each key may be associated with multiple values. You can visualize the contents of a multimap either as a map from keys to nonempty collections of values:
- a → 1, 2
- b → 3
... or as a single "flattened" collection of key-value pairs:
- a → 1
- a → 2
- b → 3
If you want to use Jackson, a popular JSON parser for Java, do the following:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new GuavaModule());
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
Multimap<String, Object> data =
mapper.readValue(json, new TypeReference<Multimap<String, Object>>() {});
Some relevant details:
GuavaModule
is a Jackson datatype collection module for supporting Guava types.
By enabling ACCEPT_SINGLE_VALUE_AS_ARRAY
, Jackson will perform auto-conversion from non-JSON-array values to single-element arrays, which is required for parsing the JSON as a Multimap<K, V>
.
The following dependencies are required:
<!-- Guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- Jackson Core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- Jackson Databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- Jackson module for Guava integration -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>${jackson.version}</version>
</dependency>
To convert the map back to a JSON document, use:
String output = mapper.writeValueAsString(data);
Upvotes: 2
Reputation: 4266
Check first if the map
contains that key already, and if so, add it.
If you're using something like HashMap<String, List<YourObject>>
:
if (map.containsKey(key)) {
map.get(key).add(yourObject);
}
Upvotes: 1