Home > front end >  Parsing json with list<Map<String, Integer>>
Parsing json with list<Map<String, Integer>>

Time:09-23

I have a JSON like this

"tags": [
                    {
                        "key": "category",
                        "value": "Prototype Pollution"
                    },
                    {
                        "key": "cvss_31_severity_rating",
                        "value": "medium"
                    },
                    {
                        "key": "cvss_score",
                        "value": "4"
                    },
                    {
                        "key": "cwe_category",
                        "value": "1321"
                    },
                    {
                        "key": "language",
                        "value": "javascript"
                    },
                    {
                        "key": "owasp_2021_category",
                        "value": "a03-injection"
                    },
                    {
                        "key": "owasp_category",
                        "value": "a03-2021-injection"
                    },
                    {
                        "key": "owasp_category",
                        "value": "a1-injection"
                    },
                    {
                        "key": "severity",
                        "value": "moderate"
                    },
                    {
                        "key": "sink_method",
                        "value": "Object.assign"
                    },
                    {
                        "key": "source_method",
                        "value": "^axios^.post"
                    }
                ],

I want to pick up the value that corresponds to "key": cvss_score. I tried picking up as a

list<Map<String, String>> map =response.getBody.jsonPath.getList("tags");

but not able to parse it correctly can someone please tell me how to do it correctly

CodePudding user response:

If you use Genson, there is an example of parsing JSON into plain Java Collections, which is quite close to what you mention:

http://genson.io/GettingStarted/#java-collections

In essence, your code would look like:

Genson genson = new Genson();
List<Object> persons = genson.deserialize("[{\"age\":28,\"name\":\"Foo\"}]", List.class);

CodePudding user response:

You can used simple JSON library and you can achieve something as follows

private Map<String, String> getTagsMap(String json){
    JSONArray tagsArray  = new JSONObject(json).getJSONArray("tags");
    Map<String, String> map = new HashMap<>();
    
    for(int i = 0; i < tagsArray.length(); i  ) {
        map.put(tagsArray.getJSONObject(i).getString("key"), tagsArray.getJSONObject(i).getString("value"));
    }
    return map;
}

Map<String, String> map = getTagsMap(json);

System.out.println(map.get("cvss_score")); //4

CodePudding user response:

If you are using Jackson library, then below snippet might help you to achieve required output.

Note: Format the JSON string as per your input, I just took a sample array as example.

    String json = "{ \"MyStringArray\" : [\"somestring1\", \"somestring2\"] }\n";
    JsonNode parent= new ObjectMapper().readTree(json);
    String content = parent.get("MyStringArray").get(0).asText();
    System.out.println(content);

CodePudding user response:

The problem is that the data structure is not as you are thinking it is to be. It is not a Map but a POJO/Bean of key/value. I have demonstrated in code using ObjectMapper:

    Map<String, String> map = new HashMap<String, String>();

    map.put("source_method", "^axios^.post");
    map.put("sink_method", "Object.assign");

    List<Map<String, String>> list = Arrays.asList(map);

    String jsonArrayOfMap = new ObjectMapper().writeValueAsString(list);:

Which produces this json(beautified):

   [
      {
        "source_method": "^axios^.post",
        "sink_method": "Object.assign"
      }
    ]

While the code you want is something like:

List<Bean> beanList = Arrays.asList(new Bean("source_method", "^axios^.post"), new Bean("sink_method", "Object.assign"));

    String jsonListOfBeans = new ObjectMapper().writeValueAsString(beanList);

    System.out.println(jsonListOfBeans)

Which produces output:

[
  {
    "value": "^axios^.post",
    "key": "source_method"
  },
  {
    "value": "Object.assign",
    "key": "sink_method"
  }
]

Bean Class:

public class Bean
{
    String key;
    String value;

    public Bean()
    {
    }

    public Bean(String key, String value)
    {
        this.key = key;
        this.value = 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;
    }

}
  • Related