0

Basically i am firing a set of APIs using GET call which returns data in json, and i want to print only specific String like hostid as mentioned in below json snippet.

  "hostId" : 286,
  "parentSectionName" : "adadfr",
  "rank" : 86096.0,
  "activationStatus" : "ACTIVE",
  "overrideLinkProp" : 0,
1
  • There are a variety of libraries that you can use to parse JSON depending on what language you're using. Pick one that looks good to you. It might be part of your selenium tests, but you won't use the selenium library for dealing with JSON objects. Commented Feb 24, 2017 at 20:46

2 Answers 2

0

Assuming that your json looks like this:

        String jsonToDecode =   "{" +
         " \"hostId\" : 286,\n" +
            "  \"parentSectionName\" : \"adadfr\",\n" +
            "  \"rank\" : 86096.0,\n" +
            "  \"activationStatus\" : \"ACTIVE\",\n" +
            "  \"overrideLinkProp\" : 0,"+
            "}";

You can decode it using "com.googlecode.json-simple" package like so:

    JSONParser parser = new JSONParser();
    Object parsedJson = new Object();
    try {
        parsedJson  = parser.parse(jsonToDecode);
    } catch (ParseException e) {
        //do something when json parsing fails
    }
    JSONObject jsonObject = (JSONObject) parsedJson;
    String name = (String) jsonObject.get("parentSectionName");
    System.out.print(name); // will output adadfr
Sign up to request clarification or add additional context in comments.

Comments

0

In java you can use JsonPath:

String jsonToDecode =   "{" +
         " \"hostId\" : 286,\n" +
            "  \"parentSectionName\" : \"adadfr\",\n" +
            "  \"rank\" : 86096.0,\n" +
            "  \"activationStatus\" : \"ACTIVE\",\n" +
            "  \"overrideLinkProp\" : 0,"+
            "}";
System.out.println(JsonPath.from(jsonToDecode).getString("hostId"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.