Retrieve just one field from JSON string in Java

30,090

Solution 1

So if this is your input:

{
    "data": {
        "email_address": "[email protected]"
    }
}

You first will need to make it a JSONObject:

JSONObject object = (JSONObject) new JSONParser().parse(json);

And then you can get data, another JSONObject:

JSONObject data = (JSONObject) object.get("data")

And from your data Object you can get email_address:

String email = data.get("email_address").toString();

If your input is an array of users, like this:

{
  "users": [
    {
      "data": {
        "email_address": "[email protected]"
      }
    },
    {
      "data": {
        "email_address": "[email protected]"
      }
    }
    ]
}

You can get it the same way:

JSONObject object = (JSONObject) new JSONParser().parse(json);
JSONArray users = (JSONArray) object.get("users");
JSONObject user0 = (JSONObject) users.get(0);
JSONObject user0data = (JSONObject) user0.get("data");
String email = user0data.get("email_address").toString();

First parse the whole JSON into an Object. Then get an array called users, from that array, get index 0. From that Object, get data, and then email_address

Solution 2

The other option is to use jsonpath.

Using the same Json blob as Lorant:

    {
        "data": {
            "email_address": "[email protected]"
        }
    }

You would use the following expression.

    $.data.email_address

Or if it was an array, simply.

    $.users.[data].email_address

An online tool can be used to experiment and learn the syntax, but if you know xpath it should be somewhat familiar already.

Share:
30,090

Related videos on Youtube

p0tta
Author by

p0tta

Mornings are for coffee and contemplation.

Updated on April 28, 2021

Comments

  • p0tta
    p0tta about 3 years

    Is there a way to just one field from the JSON string? My code is as follows:

    Object obj = parser.parse(resp);
    System.out.println(obj);
    JSONArray array = new JSONArray();
    array.add(obj);
    
    JSONObject obj2 = (JSONObject)array.get(0); //Getting NPE here
    //Object obj3 = obj2.get("data");
    
    System.out.println("Data: " + obj2.get("data"));
    //System.out.println("Email: " + obj3.get("email_address"));    
    

    I'm using the following libraries

    import org.json.simple.JSONObject;
    import org.json.simple.JSONArray;
    import org.json.simple.parser.ParseException;
    import org.json.simple.parser.JSONParser;
    

    From the response string resp, I just need data.email_address. I am unable to find a way to do it.

    • p0tta
      p0tta about 7 years
      Your answer is applicable to Javascript not Java. I am looking at how to do it in Java.
    • maraca
      maraca about 7 years
      There are JS libraries for Java, but it's probably not the most elegant way.
    • p0tta
      p0tta about 7 years
      What would be a good library? I am using one. The problem is, all the libraries that I have used make me want to strongly type the object but the response returns about 100 different elements and I only need one so i'm not sure what the best way to do it is.
    • maraca
      maraca about 7 years
      I only know Nashorn...
  • p0tta
    p0tta about 7 years
    My reply was too long so I updated the main post with the details. Thanks for the great answer, I believe i'm close.
  • p0tta
    p0tta about 7 years
    Thank you for a very thorough answer with different scenarios!
  • hfontanez
    hfontanez about 3 years
    That returns the value associated with the given key, not the field like the OP requested.
  • Artanis Zeratul
    Artanis Zeratul about 2 years
    is there a '.' (dot) notation? like object.get("data.email_address")?