How to check if json key exists?

So, I get some JSON values ​​from the server, but I don’t know if this will be a specific field or not.

So how:

{ "regatta_name":"ProbaRegatta", "country":"Congo", "status":"invited" } 

And sometimes an extra field of type appears:

 { "regatta_name":"ProbaRegatta", "country":"Congo", "status":"invited", "club":"somevalue" } 

I would like to check if there is a field called "club" so that when parsing I will not get org.json.JSONException: value for club

+138
java json android
Jul 05 '13 at
source share
12 answers

The JSONObject class has a method called "has":

http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)

Returns true if this object has a match for the name. The mapping may be NULL.

+238
Jul 05 '13 at 10:57 on
source share

You can check this path where 'HAS' - returns true if this object has a name mapping. The mapping may be NULL.

 if (json.has("status")) { String status = json.getString("status")); } if (json.has("club")) { String club = json.getString("club")); } 

You can also check using 'isNull' - Returns true if this object does not have a name match or if it has a null match.

 if (!json.isNull("club")) String club = json.getString("club")); 
+138
Jul 05 '13 at 11:07 on
source share

you could JSONObject#has by providing key input and checking if the method returns true or false . you also can

use optString instead of getString :

Returns the value displayed by name, if it exists, forcing it if necessary. Returns an empty string if such a mapping does not exist.

+15
Jul 05 '13 at
source share

You can use has

 public boolean has(String key) 

Determine if JSONObject contains a special key .

Example

 JSONObject JsonObj = new JSONObject(Your_API_STRING); //JSONObject is an unordered collection of name/value pairs if (JsonObj.has("address")) { //Checking address Key Present or not String get_address = JsonObj .getString("address"); // Present Key } else { //Do Your Staff } 
+9
Jul 23 '16 at 8:41
source share

just before you read the key, check, as before reading

 JSONObject json_obj=new JSONObject(yourjsonstr); if(!json_obj.isNull("club")) { //it contain value to be read operation } else { //it not contain key club or isnull so do this operation here } 

isNull function definition

 Returns true if this object has no mapping for name or if it has a mapping whose value is NULL. 

official documentation at the link below for isNull function

http://developer.android.com/reference/org/json/JSONObject.html#isNull(java.lang.String)

+8
Jul 05 '13 at 10:59 on
source share

try it

 if(!jsonObj.isNull("club")){ jsonObj.getString("club"); } 
+2
Jul 05 '13 at 11:10
source share

Better, instead of using a conditional type:

 if (json.has("club")) { String club = json.getString("club")); } 

- just use the existing optString () method, for example:

 String club = json.optString("club); 

The optString ("key") method returns an empty string if the key does not exist and therefore will not throw an exception.

+2
Nov 22 '17 at 23:34 on
source share
 I used hasOwnProperty('club') var myobj = { "regatta_name":"ProbaRegatta", "country":"Congo", "status":"invited" }; if ( myobj.hasOwnProperty("club")) // do something with club (will be false with above data) var data = myobj.club; if ( myobj.hasOwnProperty("status")) // do something with the status field. (will be true with above ..) var data = myobj.status; 

works in all current browsers.

+1
May 19 '18 at 2:59
source share

Try it:

 let json=yourJson if(json.hasOwnProperty(yourKey)){ value=json[yourKey] } 
+1
Aug 07 '19 at 13:27
source share

I just add one more thing. If you just want to check if something was created in JSONObject or not, you can use length (), because by default when JSONObject is initialized and the key is not inserted, it just has empty brackets {} , and use has (string key ) makes no sense.

So you can directly write if (jsonObject.length() > 0) and do your own thing.

Happy learning!

0
Aug 6
source share

You can try this to check if the key exists or not:

 JSONObject object = new JSONObject(jsonfile); if (object.containskey("key")) { object.get("key"); //etc. etc. } 
0
Aug 24 '19 at 21:05
source share

Json has a method called containsKey() .

You can use it to check if a specific key is contained in a Json set.

 File jsonInputFile = new File("jsonFile.json"); InputStream is = new FileInputStream(jsonInputFile); JsonReader reader = Json.createReader(is); JsonObject frameObj = reader.readObject(); reader.close(); if frameObj.containsKey("person") { //Do stuff } 
0
Sep 24 '19 at 13:17
source share



All Articles