Retrieve just one field from JSON string in Java
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.
Related videos on Youtube
Comments
-
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 about 7 yearsYour answer is applicable to Javascript not Java. I am looking at how to do it in Java.
-
maraca about 7 yearsThere are JS libraries for Java, but it's probably not the most elegant way.
-
p0tta about 7 yearsWhat 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 about 7 yearsI only know Nashorn...
-
-
p0tta about 7 yearsMy reply was too long so I updated the main post with the details. Thanks for the great answer, I believe i'm close.
-
p0tta about 7 yearsThank you for a very thorough answer with different scenarios!
-
hfontanez about 3 yearsThat returns the value associated with the given key, not the field like the OP requested.
-
Artanis Zeratul about 2 yearsis there a '.' (dot) notation? like object.get("data.email_address")?