Deserialize JSON object with blank array

jared

I'm having a little trouble deserializing a JSON object when there's an empty/null property (using JSON.NET), and hoping someone can point me in the right direction. Below is a snippet of code I'm trying, and been testing at dotnetfiddle

Here's a sample of the JSON:

{
    "`LCA0009": [],
    "`LCA0001": {
        "23225007190002": "1",
        "23249206670003": "1",
        "01365100070018": "5"
    },
    "`LCA0003": {
        "23331406670018": "1",
        "24942506670004": "1"
    },
    "`LCA0005": {
        "01365100070018": "19"
    }
}

I'm trying to use this code:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {

        string json = "{\"`LCA0009\": [], \"`LCA0001\": {\"23225007190002\": \"1\",\"23249206670003\": \"1\",\"01365100070018\": \"5\"},\"`LCA0003\": {\"23331406670018\": \"1\",\"24942506670004\": \"1\"},\"`LCA0005\": {\"01365100070018\": \"19\"}}";
        Console.WriteLine(json);
        Console.WriteLine();

        Console.WriteLine("This works");
        var root = JsonConvert.DeserializeObject(json);
        Console.WriteLine(root);

        Console.WriteLine("This doesn't work");
        var root2 = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, int>>>(json);
        Console.WriteLine(root2);

        foreach (var locationKvp in root2)
        {
            foreach (var skuKvp in locationKvp.Value)
            {
                Console.WriteLine("location: " + locationKvp.Key + ", sku: " +  skuKvp.Key + ", qty: " + skuKvp.Value);
            }
        }
    }
}

The "doesn't work" above is that I get this error:

Run-time exception (line 19): Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.Int32]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '`LCA0009', line 1, position 14.

How can I remove the properties that have a null/empty array?

T.J. Crowder

Fundamentally, the JSON is inconsistent and the best answer is to make it consistent. Whoever is giving you that JSON needs to be told to fix it.

Until/unless you can do that, you can convert the "raw" version to Dictionary<string, Dictionary<string, int>> yourself, but it's more work:

https://dotnetfiddle.net/zdeOOX

string json = "{\"`LCA0009\": [], \"`LCA0001\": {\"23225007190002\": \"1\",\"23249206670003\": \"1\",\"01365100070018\": \"5\"},\"`LCA0003\": {\"23331406670018\": \"1\",\"24942506670004\": \"1\"},\"`LCA0005\": {\"01365100070018\": \"19\"}}";

// Convert it
var root = (JObject)JsonConvert.DeserializeObject(json);
var results = new Dictionary<string, Dictionary<string,int>>();
foreach (var entry in root)
{
    var dict = new Dictionary<string,int>();
    if (!(entry.Value is JArray))
    {
        foreach (var subentry in (JObject)entry.Value)
        {
            int v;
            if (int.TryParse(((JValue)subentry.Value).ToString(), out v))
            {
                dict.Add(subentry.Key, v);
            }
        }
    }
    results.Add(entry.Key, dict);
}

// Results:
foreach (var name in results.Keys)
{
    var entry = results[name];
    Console.WriteLine(name + ":");
    foreach (var entryKey in entry.Keys)
    {
        Console.WriteLine("- " + entryKey + ": " + entry[entryKey]);
    }
}

I expect that can be made much more elegant with Linq.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Deserialize JSON as object or array with JSON.Net

From Dev

Deserialize array of json objects with json object inside

From Dev

Gson deserialize JSON array with multiple object types

From Dev

Deserialize JSON structure to object array and dictionary

From Dev

Json newtonsoft : Deserialize string array from an Object

From Dev

JSON to a JSON array Cannot deserialize the current JSON object

From Dev

Deserialize XML to Object Array

From Dev

Deserialize JSON Object into Class

From Dev

Deserialize Json to Object in a constructor

From Dev

Deserialize Json object into a list?

From Dev

Cannot deserialize Json to object

From Dev

deserialize inner JSON object

From Dev

JSON Convert Deserialize Object

From Dev

Deserialize stringified JSON object

From Dev

Deserialize Json to Object Exception

From Dev

JSON Deserialize Object HTTPWebResponse

From Dev

JSON Deserialize Object not working

From Dev

Json deserialize object empty

From Dev

DeSerialize Json into Class object

From Dev

Cannot deserialize a JSON object

From Dev

Deserialize JSON object

From Dev

Newton JSON deserialize to an object

From Dev

Deserialize Json to Object in a constructor

From Dev

Json Deserialize Object crashing

From Dev

Deserialize array Object using Newtonsoft Json.Net

From Dev

Jackson JSON how to deserialize date from object-array

From Dev

Deserialize JSON when a value can be an object or an empty array

From Dev

Can Spring MVC deserialize JSON which can be object or array?

From Dev

How to deserialize a JSON array containing different data types to a single object