-1

Getting results back from Shopify's graphql which it not in a standard structure to be able to deserialize simply.

Here's the result, but note the list of item1, item2, etc which can be from 1 to 100 items returned and this is the part I'm not sure how to deserialize and is my main question. Specifically, to a strongly typed List<Item> collection of items such that I can they query for any UserErrors, i.e. something like: lstItems.Any(l => l.UserErrors.Any()).

The second issue is that data will not always have these contents...other GraphQL queries will have different responses. Perhaps in this case, I should rename data in the string to another class name that will have these contents, then deserialize?

{
    "data":{
        "item1":{
            "userErrors":[
                
            ]
        },
        "item2":{
            "userErrors":[
                
            ]
        }
    },
    "extensions":{
        ...
    }
}

Here's what QuickType comes up with, but again, it assumes a discrete list of Items:

namespace QuickType
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class InventoryUpdateResult
    {
        [JsonProperty("data")]
        public Data Data { get; set; }

        [JsonProperty("extensions")]
        public Extensions Extensions { get; set; }
    }

    public partial class Data
    {
        [JsonProperty("item1")]
        public Item Item1 { get; set; }

        [JsonProperty("item2")]
        public Item Item2 { get; set; }
    }

    public partial class Item
    {
        [JsonProperty("userErrors")]
        public UserError[] UserErrors { get; set; }
    }

    public partial class UserError
    {
        [JsonProperty("field")]
        public string[] Field { get; set; }

        [JsonProperty("message")]
        public string Message { get; set; }
    }

    public partial class Extensions
    {
        ....
    }
        
}
crichavin
  • 4,672
  • 10
  • 50
  • 95

1 Answers1

0

Thanks @Charlieface for pointing me in the right direction. Here's what I ultimately did. The other part I was trying to figure out, which I didn't know and articulate at the time of writing the question, was how to separate out the data portion of the response, which had this issue from the extensions portion, which is structured properly and can be simply deserialized to a class.

  • So to split out the data portion, you parse the string to a JObject
  • Then deserialize just that portion to the Dictionary as suggested
  • Then you can iterate through the Dictionary to get all the standard class objects and do as you with with them (add them to their own list, etc.)

code:

var jsonObject = JObject.Parse(response);
Dictionary<string, InventoryUpdateResult> dicRawResults;
var data = (JObject)jsonObject["data"];
if (data != null)
{
    dicRawResults = JsonConvert.DeserializeObject<Dictionary<string, InventoryUpdateResult>>(data.ToString());
    foreach(var result in dicRawResults)
    {
        InventoryUpdateResult inventoryUpdateResult = result.Value;
    }
}

Then I extracted the extensions portion and converted that to the class object as such:

jsonObject = JObject.Parse(shopifyResponse.Value);
var jExtension = (JObject)jsonObject["extensions"];
if (jExtension != null)
{
    queryCost = jExtension.ToObject<GraphExtensions>();
    .....
}
crichavin
  • 4,672
  • 10
  • 50
  • 95