0

I am having a JSON which contains upto 1000 Keys. I need some specific keys out of it. Rather than traversing through the JSON and finding key and put its value in required parameter.

I thought of doing it in other way. I am creating a HashMap of the keys I need. Now i want to pass a JSONObject through it, where if we find the keys in JSONObject, it will automatically update the HashMap with the required keys.

Is there some function given by Spring where we can do it easily or do I Have to loop through it.

For Example:

JSONObject:-

{
"a":"a",
"b":"b",
"c":"c",
"d":"d",
"e":"e",
}

HashMap that I created :-

Map<String, Object> keys = new HashMap<>();
    keys .put("a", "");
    keys .put("b", "");

I want a function where i would pass two params

function HashMap mapJsonToHashMap(HashMap, JSONObject) {

}

Returned HashMap would be :-

{
"a":"a",
"b":"b"
}
Sudhanshu Gupta
  • 2,255
  • 3
  • 36
  • 74
  • You can use `JsonPath` library, [Iterate over a large JSON Array with JSONPath](https://stackoverflow.com/questions/55366515/iterate-over-a-large-json-array-with-jsonpath), or use `Streaming API`, [Fastest way to parse JSON from String when format is known](https://stackoverflow.com/questions/58303782/fastest-way-to-parse-json-from-string-when-format-is-known). See also: [how to parse a huge JSON file without loading it in memory](https://stackoverflow.com/questions/54817985/how-to-parse-a-huge-json-file-without-loading-it-in-memory) – Michał Ziober Nov 22 '19 at 16:13

2 Answers2

0

IMO 1000 keys are not a big deal, so I would go for a simple solution of deserialize it to an object, then just filter/map using streams. Something like:

ObjectMapper objectMapper = new ObjectMapper();

Set<String> keys = new HashSet<>();
keys.add("key-1");
keys.add("key-3");

List<Parameter> list =
    objectMapper.readValue("[{ \"key\":\"key-1\", \"value\":\"aaa\" }, { \"key\":\"key-2\", \"value\":\"bbb\" }, { \"key\":\"key-3\", \"value\":\"ccc\" }]", new TypeReference<List<Parameter>>(){});

List<Parameter> filteredList = list.stream()
    .filter(l -> keys.contains(l.getKey()))
    .collect(Collectors.toList());

// in case you really want to put results in a Map
Map<String, String> KeyValueMap = filteredList.stream()
    .collect(Collectors.toMap(Parameter::getKey, Parameter::getValue));



public class Parameter
{
    private String key;
    private String value;

    public String getKey()
    {
        return key;
    }

    public void setKey(String key)
    {
        this.key = key;
    }

    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }
}
0

You can try something like this,

  • Convert JSON to HashMap
  • Then remove unwanted entries from the converted map
public HashMap mapJsonToHashMap(HashMap keys, JSONObject json) {
// Convert JSON to HashMap
ObjectMapper mapper = new ObjectMapper();
Map<String, String> jsonMap = mapper.readValue(json, Map.class);

// Iterate jsonMap and remove invalid keys
for(Iterator<Map.Entry<String, String>> it = jsonMap .entrySet().iterator(); 
it.hasNext(); ) {
Map.Entry<String, String> entry = it.next();

if(!keys.containsKey(entry.getKey())) {
it.remove();
}  
} 

return jsonMap;
}
Santosh
  • 874
  • 11
  • 21