0

I have following array:

{
   "customizedData":[
      {
         "key":"SubscriptionId",
         "value":"xxxxxxxxxxxxxxxx"
      },
      {
         "key":"OfferId",
         "value":"xxxxxxxxxxxxxx"
      },
      {
         "key":"SubscriptionName",
         "value":"DYNAMICS 365 BUSINESS CENTRAL TEAM MEMBER"
      },
      {
         "key":"Quantity",
         "value":"6"
      },
      {
         "key":"Status",
         "value":"Suspended"
      },
      {
         "key":"PartnerOnRecord",
         "value":"None"
      }
   ]
}

How do I access key the elements:

"key": "SubscriptionName",
"value": "DYNAMICS 365 BUSINESS CENTRAL TEAM MEMBER"

At the moment I use: (string)t["customizedData"][2]["value"]

Are there any better ways?

11
  • can you share what have you tried so far? Commented Dec 15, 2020 at 11:30
  • Is it C#, did I guess correctly? Commented Dec 15, 2020 at 12:48
  • What library do you use to work with json? Is it Json.NET? Add tag please. Commented Dec 15, 2020 at 12:49
  • @AlexanderPetrov its tagged c# so I would assume so ? Commented Dec 15, 2020 at 14:07
  • Is deserializing to a strong type like public partial class KeyValueThing { [J("customizedData")] public List<CustomizedDatum> CustomizedData { get; set; } } public partial class CustomizedDatum { [J("key")] public string Key { get; set; } [J("value")] public string Value { get; set; } } and simply filtering like KeyValueThing.CustomizedData.FirstOrDefault(x=> x.Key=="MyKey") viable? Commented Dec 15, 2020 at 15:01

2 Answers 2

1

Assuming you are using Json.net and you don't want to create strong type for it so you can use SelectToken with JSONPath:

t.SelectToken("$.customizedData[?(@.key == 'SubscriptionName')].value").ToString();
Sign up to request clarification or add additional context in comments.

Comments

1

If you are using the Newtonsoft.Json nuget this could be done with a helper function. first use var jo = JObject.Parse(jsonContent); to parse, and pass that to the helper function GetValueFromJson(jo, "SubscriptionName", "customizedData"), using this helper function:

    private string GetValueFromJson(JObject jo, string key, string rootKey)
    {
        var value = jo[rootKey].Children()
                                        .Where(i => i["key"].ToString() == key)
                                        .Select(i => i["value"].ToString())
                                        .FirstOrDefault();
        return value;
    }

1 Comment

This isn't bad, but I think it would be better to pass in the JObject as the first parameter. Otherwise if you want to use the method multiple times on the same JSON for different keys you end up re-parsing the JSON each time. Better to parse once and then extract, extract, extract.

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.