Representing null in JSON - json

What is the preferred method for returning null values in JSON? Is there a different preference for primitives?
For example, if my object on the server has an Integer called "myCount" with no value, the most correct JSON for that value would be:
{}
or
{
"myCount": null
}
or
{
"myCount": 0
}
Same question for Strings - if I have a null string "myString" on the server, is the best JSON:
{}
or
{
"myString": null
}
or
{
"myString": ""
}
or (lord help me)
{
"myString": "null"
}
I like the convention for collections to be represented in the JSON as an empty collection http://jtechies.blogspot.nl/2012/07/item-43-return-empty-arrays-or.html
An empty Array would be represented:
{
"myArray": []
}
EDIT Summary
The 'personal preference' argument seems realistic, but short sighted in that, as a community we will be consuming an ever greater number of disparate services/sources. Conventions for JSON structure would help normalize consumption and reuse of said services. As far as establishing a standard, I would suggest adopting most of the Jackson conventions with a few exceptions:
Objects are preferred over primitives.
Empty collections are preferred over null.
Objects with no value are represented as null.
Primitives return their value.
If you are returning a JSON object with mostly null values, you may have a candidate for refactoring into multiple services.
{
"value1": null,
"value2": null,
"text1": null,
"text2": "hello",
"intValue": 0, //use primitive only if you are absolutely sure the answer is 0
"myList": [],
"myEmptyList": null, //NOT BEST PRACTICE - return [] instead
"boolean1": null, //use primitive only if you are absolutely sure the answer is true/false
"littleboolean": false
}
The above JSON was generated from the following Java class.
package jackson;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonApp {
public static class Data {
public Integer value1;
public Integer value2;
public String text1;
public String text2 = "hello";
public int intValue;
public List<Object> myList = new ArrayList<Object>();
public List<Object> myEmptyList;
public Boolean boolean1;
public boolean littleboolean;
}
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(new Data()));
}
}
Maven dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.0</version>
</dependency>

Let's evaluate the parsing of each:
http://jsfiddle.net/brandonscript/Y2dGv/
var json1 = '{}';
var json2 = '{"myCount": null}';
var json3 = '{"myCount": 0}';
var json4 = '{"myString": ""}';
var json5 = '{"myString": "null"}';
var json6 = '{"myArray": []}';
console.log(JSON.parse(json1)); // {}
console.log(JSON.parse(json2)); // {myCount: null}
console.log(JSON.parse(json3)); // {myCount: 0}
console.log(JSON.parse(json4)); // {myString: ""}
console.log(JSON.parse(json5)); // {myString: "null"}
console.log(JSON.parse(json6)); // {myArray: []}
The tl;dr here:
The fragment in the json2 variable is the way the JSON spec indicates null should be represented. But as always, it depends on what you're doing -- sometimes the "right" way to do it doesn't always work for your situation. Use your judgement and make an informed decision.
JSON1 {}
This returns an empty object. There is no data there, and it's only going to tell you that whatever key you're looking for (be it myCount or something else) is of type undefined.
JSON2 {"myCount": null}
In this case, myCount is actually defined, albeit its value is null. This is not the same as both "not undefined and not null", and if you were testing for one condition or the other, this might succeed whereas JSON1 would fail.
This is the definitive way to represent null per the JSON spec.
JSON3 {"myCount": 0}
In this case, myCount is 0. That's not the same as null, and it's not the same as false. If your conditional statement evaluates myCount > 0, then this might be worthwhile to have. Moreover, if you're running calculations based on the value here, 0 could be useful. If you're trying to test for null however, this is actually not going to work at all.
JSON4 {"myString": ""}
In this case, you're getting an empty string. Again, as with JSON2, it's defined, but it's empty. You could test for if (obj.myString == "") but you could not test for null or undefined.
JSON5 {"myString": "null"}
This is probably going to get you in trouble, because you're setting the string value to null; in this case, obj.myString == "null" however it is not == null.
JSON6 {"myArray": []}
This will tell you that your array myArray exists, but it's empty. This is useful if you're trying to perform a count or evaluation on myArray. For instance, say you wanted to evaluate the number of photos a user posted - you could do myArray.length and it would return 0: defined, but no photos posted.

null is not zero. It is not a value, per se: it is a value outside the domain of the variable indicating missing or unknown data.
There is only one way to represent null in JSON. Per the specs (RFC 4627 and json.org):
2.1. Values
A JSON value MUST be an object, array, number, or string, or one of
the following three literal names:
false null true

There is only one way to represent null; that is with null.
console.log(null === null); // true
console.log(null === true); // false
console.log(null === false); // false
console.log(null === 'null'); // false
console.log(null === "null"); // false
console.log(null === ""); // false
console.log(null === []); // false
console.log(null === 0); // false
That is to say; if any of the clients that consume your JSON representation use the === operator; it could be a problem for them.
no value
If you want to convey that you have an object whose attribute myCount has no value:
{ "myCount": null }
no attribute / missing attribute
What if you to convey that you have an object with no attributes:
{}
Client code will try to access myCount and get undefined; it's not there.
empty collection
What if you to convey that you have an object with an attribute myCount that is an empty list:
{ "myCount": [] }

I would use null to show that there is no value for that particular key. For example, use null to represent that "number of devices in your household connects to internet" is unknown.
On the other hand, use {} if that particular key is not applicable. For example, you should not show a count, even if null, to the question "number of cars that has active internet connection" is asked to someone who does not own any cars.
I would avoid defaulting any value unless that default makes sense. While you may decide to use null to represent no value, certainly never use "null" to do so.

I would pick "default" for data type of variable (null for strings/objects, 0 for numbers), but indeed check what code that will consume the object expects. Don't forget there there is sometimes distinction between null/default vs. "not present".
Check out null object pattern - sometimes it is better to pass some special object instead of null (i.e. [] array instead of null for arrays or "" for strings).

According to the JSON spec, the outermost container does not have to be a dictionary (or 'object') as implied in most of the comments above. It can also be a list or a bare value (i.e. string, number, boolean or null). If you want to represent a null value in JSON, the entire JSON string (excluding the quotes containing the JSON string) is simply null. No braces, no brackets, no quotes. You could specify a dictionary containing a key with a null value ({"key1":null}), or a list with a null value ([null]), but these are not null values themselves - they are proper dictionaries and lists. Similarly, an empty dictionary ({}) or an empty list ([]) are perfectly fine, but aren't null either.
In Python:
>>> print json.loads('{"key1":null}')
{u'key1': None}
>>> print json.loads('[null]')
[None]
>>> print json.loads('[]')
[]
>>> print json.loads('{}')
{}
>>> print json.loads('null')
None

This is a personal and situational choice. The important thing to remember is that the empty string and the number zero are conceptually distinct from null.
In the case of a count you probably always want some valid number (unless the count is unknown or undefined), but in the case of strings, who knows? The empty string could mean something in your application. Or maybe it doesn't. That's up to you to decide.

'null' is best for practical use
FWIW, using PHP as an example, PHP interprets empty sets as entries made by PHP...
// This loop will iterate one item with the value 'the car'
$empty_json = '["the car"]';
$some_json_array = json_decode($empty_json);
foreach ($some_json_array as $i) {
echo "PHP sees one item";
}
output: PHP sees the car
// This loop will iterate one item, but with no values
$empty_json = '[""]';
$some_json_array = json_decode($empty_json);
foreach ($some_json_array as $i) {
echo "PHP sees: $i";
}
output: PHP sees
// This loop will iterate nothing because PHP's `json_decode()` function knew it was `null`
$empty_json = 'null';
$some_json_array = json_decode($empty_json);
foreach ($some_json_array as $i) {
echo "PHP sees one item";
}
output: (nothing, foreach will not loop)

Related

Handle JSON With null Inside Array With Kotlin

I am trying to handle null or empty JSON value fields value which has received both JSON case:
{
"field": [
null
]
}
and
{
"field": []
}
The case when an empty array works fine for me: If I get an object with an array size of 0, it means it is empty. In the second case, when the field's value is [null], I get an array size 1 with all the elements null. That is why I check the null case with the following approach:
val deserializedJson = jacksonObjectMapper().readValue<DataSimpleClass>(theJsonAsText)
if (deserializedJson.field.size == 1 && deserializedJson.field[0] == null) {
throw Exception()
}
Is there any better or more elegant approach to check such a [null] case?
I deserialize the JSON using jacksonObjectMapper() object version 2.9.8. Also, I have created a two-data class that looks like that:
#JsonInclude(JsonInclude.Include.NON_NULL)
data class DataSimpleClass(
#JsonInclude(JsonInclude.Include.NON_NULL)
val field: List<SpecificClass>
)
#JsonInclude(JsonInclude.Include.NON_NULL)
data class SpecificClass(
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonProperty("field1") val field1: String,
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonProperty("field2") val field2: String,
#JsonProperty("field3") val field3: String? = null,
val updateTime: Instant = Instant.now(Clock.systemUTC())
)
Also, I don't understand how Kotlin (null-safe language) may let me create a field when all the elements are null. How is it possible that Kotlin doesn't catch null while I send the non-nullable field null value The deserializedJson result, then the JSON key's value is null ?
I was expecting that the null won't be deserialized cause of its null value, as the object DataSimpleClass holds a non-nullable field.
In addition, my IntelliJ shows me that due to the null safe fields, the following condition "is always false" while, in fact, it is actually true during the run.
How is it possible that the value shouldn't be null due to the null safe, but it all gets null during the run time?the IntelliJ warning for wrong condition result
Kotlin is "null-safe" language because it enforces non-null-ability by default.
You can still have nulls in Kotlin - e.g. val foo: String? = null
The IDE just says that based on your definition it shouldn't be null and it will not allow you(at compile time) to put null there. Runtime is where don't have control over who/what puts null there.
If there is no guarantee that you will not receive null there you should sanitize it before assuming there are no nulls.
deserializedJson.field..filterNotNull()
If you would rather that it crashed the whole app I think you can set
.addModule(KotlinModule(strictNullChecks = true))
when configuring Jackson.

Hamcrest Contains Matcher

So my problem is presenting as a type-matching of sorts; I have code that queries a database, and returns an array of string type. When I attempt to validate against my JSON message returned from a web service, one of the values is a primitive integer (without the double-quotes), and the validation is failing, as it is stating: Expected: iterable containing {"1", "1", "1", "1", "1"}Actual: [1, 1, 1, 1, 1]I'm using the contains matcher to validate a ListArray of values against many returned by the query. My assumption is that the Actual is being evaluated as an integer, but the values to validate against (Expected) are String. I've been racking my brain attempting the HasToString or hasItem matchers but I think that would just parse toString if the target is a single value.I guess my ultimate question is, is there a way to force Hamcrest to evaluate the JSON data as a String, or implicitly/explicitly cast the Expected to the evaluated type?Thanks in advance.
So, I acutally think I figured this one out; what I ended up doing was performing a toString(). on the extracted ArrayList of objects, which gave me string values; code example below:
ArrayList<String> myObj = response.path(jsonField);
String[] myObjStr = new String[myObj.size()];
int x = 0;
for (Object obj : myObj){
myObjStr[x] = obj.toString();
x++;
}
From there, I was able to compare the resulting arrays; Now if only I could figure out how to get rid of pesky angle brackets for nested elements...

How can I cast numbers propely in Scala?

I need to put some numeric value into an JSON array. But every time I use obj.put("key", value) the value is automatically "casted" into Integer.
i.e.
value = 0.0 => {"key": 0}
I want the value in the JSON object to be EXACTLY the same of the value I get from the application.
I thought of using value.getClass in order to get the value of the class and create a match...case method and use value.toString.to*, where * is the corresponding class (i.e the class is Integer, the value will be casted using value.toString.toInt). But this approach is kinda "dirty" in my opinion, and I'm new to Scala so I don't know this language very well and its tricks.
Any suggestion?
EDIT: Here's the code (here I send the JSON to a server):
for ((p, v) <- exportMap){
val obj: JSONObject = new JSONObject()
obj.put("path", parsePathString(p.toString))
if (v.toString.equals("Infinity")) {
obj.put("result", "Inf")
} else {
obj.put("result", v)
}
exportJSONArray.put(obj)
Here I get the JSON from the server, in order to use its data in the app:
var result: Any = jSONObject.get("result")
if (result.equals("Inf")) {
result = Double.PositiveInfinity
}
and then the variable "result" goes in a data structure.
So I need to have the EXACT same type of numeric data.
So I need to have the EXACT same type of numeric data
First of all, you want exact same type, but type is as Any. It's like saying that you don't care about the type. You can use optDouble getter for it:
val result: Double = jSONObject.optDouble("result", Double.PositiveInfinity)
(and it doesn't have to be a var, try not to use vars in Scala)
Next, in the part where you write to the object, you can check whether the value is infinity:
obj.put("result", if(v.isInfinity) "Inf" else v)
I think this solves your problem.
There are very many JSON libraries in Java (and Scala), and you picked one which intentionally does this:
public static String numberToString(Number number) throws JSONException {
if (number == null) {
throw new JSONException("Null pointer");
}
testValidity(number);
// Shave off trailing zeros and decimal point, if possible.
String string = number.toString();
if (string.indexOf('.') > 0 && string.indexOf('e') < 0
&& string.indexOf('E') < 0) {
while (string.endsWith("0")) {
string = string.substring(0, string.length() - 1);
}
if (string.endsWith(".")) {
string = string.substring(0, string.length() - 1);
}
}
return string;
}
You can pick a different one which doesn't.

How to process null value inside json string using lua?

I am using lua in asterisk pbx. I encounter following problem while processing json string.
json "null" value converted to function type in lua. why?
how to handle this scenario? because i am expecting nil because no value means null in json and nil means nothing in lua.
local json = require( "json" )
local inspect = require("inspect")
local myjson_str='{"Sms":{"key":"xxxxxxxxxxxxxxxxxxxxx","to":"{caller}","senderid":null,"type":"Simple","content":"Your request has been accepted in Previous Miss call. We get back to you very soon."}}'
local myjson_table = json.decode(myjson_str)
print(type(myjson_table["Sms"]["senderid"]))
print(myjson_table)
print(inspect(myjson_table))
print(json.encode(myjson_table))
out put for above is
function
table: 0xf5e770
{
Sms = {
content = "Your request has been accepted in Previous Miss call. We get back to you very soon.",
key = "xxxxxxxxxxxxxxxxxxxxx",
senderid = <function 1>,
to = "{caller}",
type = "Simple"
}
}
{"Sms":{"type":"Simple","key":"xxxxxxxxxxxxxxxxxxxxx","senderid":null,"content":"Your request has been accepted in Previous Miss call. We get back to you very soon.","to":"{caller}"}}
It is up to specific library to decide how to represent null value.
Using nil has its own problem because its not possible find either
original JSON has key with null value or there no such key at all.
So some libraries just return some unique value. Some provide
a way to pass this value like json.deconde(str, NULL_VALUE).
So answer is just read the doc/source of library you use.
Most likely it provide something like json.null value to check
either value is null. But function is really strange choice because
they have some undetermined rules of uniqueness.
Or try another library.
First of all, #moteus is right:
It is up to specific library to decide how to represent null value
If you're using the JSON library by Jeffrey Friedl the solution is to use a placeholder instead of null and serializing the table structure to a json string using designated encode options:
-- define a placeholder
NullPlaceholder = "\0"
-- use it in an internal table
tableStructure = {}
tableStructure['someNullValue'] = NullPlaceholder
-- pass the placeholder to the encode methode
encode_options = { null = NullPlaceholder }
jsonString = JSON:encode(tableStructure, nil, encode_options)
which leads to
{"someNullValue": null}

Json.Net boolean parsing issue

JObject.Parse(jsonString) is causing issue for boolean data. e.g. The json is :
{
"BoolParam": true
}
I used the following code to parse:
JObject data = JObject.Parse(str1);
foreach (var x in data)
{
string name = x.Key;
Console.Write(name + " (");
JToken value = x.Value;
Console.Write(value.Type + ")\n");
Console.WriteLine(value);
}
This print out the value as :
BoolParam (Boolean) : True
The case sensitivity causes issue as I save this json for later use. The saved json looks like
{
"BoolParam": True
}
However, when i later use it, the JObject.Parse(str) throws error as invalid Json :Unexpected character encountered while parsing value: T. Path 'BoolParam', line 2, position 15.
If I change the case from "True" to "true", it works. I dont want to add this hack to change the case when saving but is there a better way to handle this scenario.
I dont want to add this hack to change the case when saving but is
there a better way to handle this scenario.
No, you have to produce valid JSON when saving if you want to be able to later deserialize it with a JSON serializer such as Newtonsoft JSON. So fixing your saving routing is the right way to go here.
One could use anonymous types and no worry about case sensitivity of boolean type variables
public static void Main()
{
bool a = true;
JObject c = JObject.FromObject(new {BoolParam= a});
Console.WriteLine(c);
}
Output:
{
"BoolParam": true
}