Is "[]" valid JSON?

I am having trouble de-serializing this JSON string using JSON.NET (note the quotes):

"[]"

      

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

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; }
}

      

An exception

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

Is there a way to get JSON.NET to parse this?

+3


source to share


2 answers


Part 1: Is "[]" valid JSON?

There are several documents and standards for JSON and hundreds of parsers; and some of them suggest that JSON can only be an object {}

or an array []

, but some of them allow single values ​​like strings, numbers used as JSON.

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

This debate over the validity of JSON is another matter. It doesn't matter in your case, because ...

Part 2: why your code isn't working.

Even if we assume that non-objects \ non-arrays are valid JSON, your JSON is a single string equal to "[]"

. It can be anything, not parentheses, this is not array notation, but only two symbols "[" and "]".

However, you are trying to parse this JSON as an array of objects, which will fail anyway.

In other words, even if it is valid JSON, then it is a valid JSON string, not a 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);
      

Run codeHide result


Part 3: what you should do

Better idea is to stop using invalid JSON as input. Tell who gave you this JSON that it is an invalid JSON array and you cannot use it. You could deserialize the JSON into your array User

, if it was correct how you use it:

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

      

If this JSON is provided from third party services and you can't do anything about it, then you need to strip it and make it valid. Yes, unfortunately it happens sometimes. How? It depends on what your value is when there are objects (users).

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

+8


source


It's valid JSON, but the deserializer doesn't work because the data types don't match.

"[]"

      

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



[]

      

It is an empty array. In short, this should work:

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

      

+5


source







All Articles