Converting HashMap into JSONArray, Values are not coming properly

Multi tool use
Converting HashMap into JSONArray, Values are not coming properly
Am getting response from Elasticsearch with duplicates, to avoid that i used Hashmap
implementation and i put
all the values into the HashMap
object.
Hashmap
put
HashMap
After that am iterating over the HashMap
object to convert into JSONArray.
HashMap
Am geting one unique record from distinctObjects
(HashMap Object). But after if convert into JSONArray., the length of JSONArray shows 2
it suppose to be 1
and am printing the JSONArray, it shows like below.
distinctObjects
2
1
JSONArray --->[{"code":"VA1125-GGA-1","id":"code"},{"code":"12816","id":"id"}]
Expected Result should be :
JSONArray --->[{"code":"VA1125-GGA-1","id":"12816"}]
Please find my code below.
JSONObject responseObj;
JSONArray responseArray = new JSONArray();
Map<String, Object> distinctObjects = null;
SearchHit searchHits2 = searchResponse2.getHits().getHits();
for (SearchHit hit2 : searchHits2) {
Map<String, Object> sourceAsMap2 = hit2.getSourceAsMap();
distinctObjects = new HashMap<String, Object>();
distinctObjects.put("id", sourceAsMap2.get("id").toString());
distinctObjects.put("code", sourceAsMap2.get("code").toString());
}
Iterator it = distinctObjects.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
responseObj = new JSONObject();
responseObj.put("id", pair.getKey());
responseObj.put("code", pair.getValue());
responseArray.put(responseObj);
it.remove(); // avoids a ConcurrentModificationException
}
System.out.println("Link ID List Size --->"+responseArray.length());
System.out.println("JSONArray --->"+responseArray.toString());
1 Answer
1
It looks like you're adding both code
and id
as top level entries to your distinctObjects map which is why you're getting two objects back. Assuming you want to de-dup based on ID your first loop should look something like:
code
id
for (SearchHit hit2 : searchHits2) {
Map<String, Object> sourceAsMap2 = hit2.getSourceAsMap();
distinctObjects = new HashMap<String, Object>();
distinctObjects.put(sourceAsMap2.get("id"), sourceAsMap2.get("code").toString());
}
That will give you one entry in distinctObjects
for every unique id with a value of the code
.
distinctObjects
code
If you wanted you could also add sourceAsMap2
as the value in distinctObjects
to maintain the full response if you need more than just the code
in downstream processing.
sourceAsMap2
distinctObjects
code
Thanks a lot., I made, downstream Processing like this
Collection<Object> values = distinctObjects.values(); JSONArray jsonArray = new JSONArray(values); for(int i=0;i<jsonArray.length();i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); responseObj = new JSONObject(); responseObj.put("id", jsonObj.optString("id")); responseObj.put("code", jsonObj.optString("code")); responseArray.put(responseObj); }
– Karthikeyan
Jul 3 at 10:02
Collection<Object> values = distinctObjects.values(); JSONArray jsonArray = new JSONArray(values); for(int i=0;i<jsonArray.length();i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); responseObj = new JSONObject(); responseObj.put("id", jsonObj.optString("id")); responseObj.put("code", jsonObj.optString("code")); responseArray.put(responseObj); }
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.
– Hearen
Jul 3 at 9:23