Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "[]" valid JSON?

Tags:

json

c#

json.net

I'm having troubles de-serializing this JSON string using JSON.NET (note the quotes):

"[]"

Depending on which JSON validation website you go to, this is valid JSON (jsonlint for example says it is).

The JSON.NET code:

void Main()
{
    string json = "\"[]\""; 
    var x = JsonConvert.DeserializeObject<User[]>(json);
    Console.WriteLine(x);
}

// Define other methods and classes here
public class User
{
    public string Id { get; set; }
    public int Age { get; set; }
}

The exception

Error converting value "[]" to type 'UserQuery+User[]'. Path '', line 1, position 4.

Is there a way of forcing JSON.NET to parse this?

like image 978
Chris S Avatar asked Dec 05 '22 14:12

Chris S


2 Answers

Part 1: Is "[]" valid JSON?

There are several documents and standards on JSON, and hundreds of parsers; and some of them suppose that JSON can only be object {} or an array [], but some allow single values like strings, numbers to be used as JSON.

Read this article, it widely describes this problem.
What is the minimum valid JSON?

This dispute on JSON validity is another question. In your case, it doesn't matter, because...

Part 2: why your code isn't working.

Even if we allow non-objects \ non-arrays to be valid JSON, then your JSON represents a single string equal to "[]". It could be anything else, not brackets, it is not an array notation, but just two symbols "[" and "]".

However, you try to parse this JSON as an array of objects, which will anyway result into error.

In other words, even if it is a valid JSON, then it is a valid JSON string, not JSON array.

var str1 = JSON.parse("\"[]\""),
    str2 = JSON.parse("\"could be anything else, not brackets\""),
    arr = JSON.parse("[]");
    
console.log(typeof str1);
console.log(typeof str2);
console.log(typeof arr);

var str1_s = JSON.stringify([]);
console.log("Valid JSON of an empty array: " + str1_s);
var arr_s = JSON.stringify("[]");
console.log("Partly valid JSON of a string '[]': " + arr_s);

Part 3: what should you do

The best idea - stop using invalid JSON as input. Tell whoever gave you this JSON that it is invalid JSON array and you cannot use it. You would be able to deserialize a JSON into your array of User if it was correct just like you use it:

    string json = "[]"; 
    var x = JsonConvert.DeserializeObject<User[]>(json);
    Console.WriteLine(x);

If this JSON is provided from 3rd party services and you can do nothing about that, then you need to tidy it up and make it valid. Yeah, unfortunately, sometimes it happens.
How? It depends on what is your value when there ARE objects (users).

It may be a JSON-serialized JSON-string (double-serialized) like this, and then you need to deserialize a string, and then deserialize an array.
Or it can just have two odd quotes in the beginning and the end, and you can just remove them.

like image 89
Yeldar Kurmangaliyev Avatar answered Dec 22 '22 11:12

Yeldar Kurmangaliyev


It is valid JSON, but the deserializer failes because the datatypes do not match.

"[]"

Is a string, so the deserializer wants to serialize it to a string.

[]

Is an empty array. So, in short, this should work:

string json = "[]"; 
var x = JsonConvert.DeserializeObject<User[]>(json);
Console.WriteLine(x);
like image 27
MarkO Avatar answered Dec 22 '22 13:12

MarkO