Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json and empty array

Tags:

json

arrays

I have the following json :

{    "users":     [{        "user":          {         "user_id"  :"a11uhk22hsd3jbskj",         "username" :"tom",         "location" : null         }     }] } 

I get this json in response to a request to an api. Looking at the doc for this api, location is supposed to be an array (containing geodata, latitude, longitude address etc).
Now the question is : there's an error in the json? I mean, location doesn't seems,to me, to be an array, or is possible to represent a null array in that way? and if yes what is the difference between :

"location" : null  "location" : [] 

Thanks in advance

like image 475
oiledCode Avatar asked Nov 12 '11 12:11

oiledCode


People also ask

Can you have an empty array in JSON?

JSON data has the concept of null and empty arrays and objects.

Does JSON allow empty string?

In Spark 3.0 and above, the JSON parser does not allow empty strings. An exception is thrown for all data types, except BinaryType and StringType .

Is empty string null in JSON?

Strings, Booleans and integers do not have an 'empty' form, so there it is okay to use null values.

Can a JSON file be just an array?

JSON can actually take the form of any data type that is valid for inclusion inside JSON, not just arrays or objects.


Video Answer


2 Answers

"location" : null // this is not really an array it's a null object "location" : []   // this is an empty array 

It looks like this API returns null when there is no location defined - instead of returning an empty array, not too unusual really - but they should tell you if they're going to do this.

like image 159
Matt Varblow Avatar answered Sep 26 '22 00:09

Matt Varblow


null is a legal value (and reserved word) in JSON, but some environments do not have a "NULL" object (as opposed to a NULL value) and hence cannot accurately represent the JSON null. So they will sometimes represent it as an empty array.

Whether null is a legal value in that particular element of that particular API is entirely up to the API designer.

like image 34
Hot Licks Avatar answered Sep 25 '22 00:09

Hot Licks