"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.
Answer from Matt Varblow on Stack Overflow"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.
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.
What to return if a collection is empty?
How to assert json with empty array
Best Practices to send for Empty Values of a JSON API?
An empty array in JSON configuration file results in a null value in the configuration object
Assuming to be placed in a variable, because you have 'json' tagged. The following is some explanation of each step of a simple example breakdown:
// create an empty array
var array = [];
// create an object with properties "id", "action" with mock values
var object = {
id: "1",
action: "shout"
}
// add object to array
array.push(object);
// create encode json string
var myJSONString = JSON.stringify(array);
Hope this helps.
Do you mean this?
JSON.stringify({
id: null,
action: null
})
What do you choose? Getting lots of conflicting answers.
-
Null
-
Undefined (Omitted from JSON)
-
Empty of that type?
-
String = ""
-
Number = 0
-
Boolean = false
-
Array = []
-
Object = {}
-
CMS JSON API to be consumed by JS Framework Frontend
If you want to have empty space in JSON, you should fill it with null.
Example:
var pattern = [ ["C5", 3], null, null, null, null, null, null, null, ["C5", 3], null, null, null, null, ...... ]
If you don't want to use null with a sparse array, you could use the object representation of an array.
pattern = [["C5", 3], , , , , , , , ["C5", 3], , , ]
... would be replaced by an object with index value as properties and an additional length property:
parse_array = {
0: ["C5", 3],
8: ["C5", 3],
length: 12
}
This object can be generated from the array using its reduce method:
parse_array = pattern.reduce( ( obj, val, i ) =>
{
obj.length++
if ( val )
obj[i] = val
return obj
}, { length: 0 } )
Then it's easy to convert the object into a JavaScript Array, using a simple arrow function:
pattern = Array.from( parse_array, v => v )
//Encode array
var pattern = [["C5",3],null,null,null,null,null,null,null,["C5",3],null,null,null]
out.innerHTML = JSON.stringify( pattern ) + "\n"
var parse1 = pattern.reduce( ( obj, val, i ) =>
{
obj.length++
if ( val )
obj[i] = val
return obj
}, { length } )
out.innerHTML += JSON.stringify( parse1 ) + "\n"
//Decode object
var parse2 = {
0: ["C5", 3],
8: ["C5", 3],
length: 12
}
out.innerHTML += JSON.stringify( parse2 ) + "\n"
out.innerHTML += JSON.stringify( Array.from( parse2, v => v ) ) + "\n"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<h3>JSON array with empty items</h3>
<pre id="out"></pre>
</body>
</html>
If the array is defined in the file but is empty, like:
...
"kl":[]
...
Then getJSONArray("kl") will return an empty array, but the object is not null. Then, if you do this:
kl = c.getJSONArray("kl");
if(kl != null){
klassenID[i] = kl.getJSONObject(0).getString("id");
}
kl is not null and kl.getJSONObject(0) will throw an exception - there is no first element in the array.
Instead you can check the length(), e.g.:
kl = c.getJSONArray("kl");
if(kl != null && kl.length() > 0 ){
klassenID[i] = kl.getJSONObject(0).getString("id");
}
You can also use isEmpty() method, this is the method we use to check whether the list is empty or not. This method returns a Boolean value. It returns true if the list is empty otherwise it gives false. For example:
if (!k1.isEmpty()) {
klassenID[i] = kl.getJSONObject(0).getString("id");
}