0

I have a JSON being received from a public API that follows the structure below.

   [
     {
"ID": "12345",
"company": [
  {
    "contract_ID": "abc5678",
    "company": [
      {
        "company_name": "HelloWorld",
        "company_logo": "HW",
        "invested": "2000"
      }
    ]
  },
  {
    "contract_ID": "67891",
    "company": [
      {
        "company_name": "GoodBye",
        "company_logo": "GB",
        "invested": "500"
      }
    ]
  },
  {
    "contract_ID": "32658",
    "company": [
      {
        "company_name": "YesNo",
        "company_logo": "YN",
        "invested": "1500"
      }
    ]
  }
]
   }
     ]

I've tried several different methods of parsing, whether it be JTokens/JArrays or various classes. Something like the following allows me to access the first group of values (company_name, company_logo, invested), but I cannot iterate through to find the other two.

            //receiving the json data
            var responseString = await response.Content.ReadAsStringAsync(); 
            var fullJSON = JsonConvert.DeserializeObject(responseString);
            Debug.Log(fullJSON);

            //trying to parse it down to just the data I need
            JToken token = JToken.Parse("{\"root\":" + responseString + "}");
            Debug.Log(token);
            JArray assets = (JArray)token.SelectToken("root[0].assets");
            Debug.Log(assets);
            JToken dig = JToken.Parse("{\"root\":" + assets + "}");
            Debug.Log(dig);
            JArray assetsNested = (JArray)dig.SelectToken("root[0].assets");
            Debug.Log(assetsNested);

My goal is to extract the contract_ID and then the associated company_name, company_logo, and invested items. For example, abc5678, HelloWorld, HW, and 2000 would be one of the three datasets needed.

1
  • It would definitely be easier if you just deserialized to some C# classes that match the shape of your JSON. Commented Nov 2, 2022 at 4:53

1 Answer 1

3

The easiest way to deserialize this properly and keep your code maintainable is to use concrete classes. For example:

public sealed class Company
{
    [JsonProperty("contract_ID")]
    public string ContractID { get; set; }

    [JsonProperty("company")]
    public List<CompanyDefinition> CompanyDefinitions { get; set; }
}

public sealed class CompanyDefinition
{
    [JsonProperty("company_name")]
    public string CompanyName { get; set; }

    [JsonProperty("company_logo")]
    public string CompanyLogo { get; set; }

    [JsonProperty("invested")]
    public string Invested { get; set; }
}

public sealed class RootCompany
{
    [JsonProperty("ID")]
    public string ID { get; set; }

    [JsonProperty("company")]
    public List<Company> Company { get; set; }
}

Then, you simply deserialize. For example, if you are pulling from a file you could do this:

using(var sr = new StreamReader(@"c:\path\to\json.json"))
using(var jtr = new JsonTextReader(sr))
{
    var result = new JsonSerializer().Deserialize<RootCompany[]>(jtr);

    foreach(var r in result)
    {
        Console.WriteLine($"Company ID: {r.ID}");
        foreach(var c in r.Company)
        {
            Console.WriteLine($"ContractID: {c.ContractID}");
            foreach(var d in c.CompanyDefinitions)
            {
                Console.WriteLine($"Name: {d.CompanyName}");
                Console.WriteLine($"Invested: {d.Invested}");
                Console.WriteLine($"Company Logo: {d.CompanyLogo}");
            }
        }
    }
}

When something changes with your models, you won't have to dig through lines upon lines of hard-to-read token selection code. You just update your models.

Sign up to request clarification or add additional context in comments.

4 Comments

This was extremely helpful - I am new to net related coding and there seem to be a lot of different approaches out there. Thank you for your help and time!
As an aside for anyone else looking to get started, this helped me: c-sharpcorner.com/article/from-zero-to-hero-in-json-with-c-shar
In VS you can under the Edit meny go to "Paste special" and select "Paste JSON as Classes". In this way you can easily create classes for deserializing your JSON body.
@K.J.M.O. -- Whoa... I've been using 3rd party websites to do this. That's pretty sweet. Thanks for the tip!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.