0

I have the following JSON string, and I need to access the property value named "Mode" from it:

{
  "CommomData": {
    "DateTime": {
      "Year": 2019,
      "Month": 3,
      "Day": 11,
      "Hour": 14,
      "Min": 1,
      "Second": 29
    }
  },
  "Status": {
    "Mode": "Test",
    "Loc": "Test"
  }
}

If you note here, the parent property name for "Mode", here is "Status" but this could change to "LatestStatus" or "FirstStatus" or any other value.

Currently, I have written the code below and it works fine, but it takes around 60 to 150 milliseconds for the operation (we want to reduce this time). Please note that the object has many more properties, but I only posted a few to explain the issue.

Is there any other optimal way to get the value from the JSON string without knowing the object type and the parent property name?

JObject payloadJObject = JObject.Parse(payload);

foreach (var item in payloadJObject)
{
    foreach (JProperty subitem in item.Value.ToList())
    {
        if (subitem.Name == "Mode")
        {
            return Convert.ToString(subitem.Value);
        }
    }
}
4
  • What language is this…? Commented Mar 13, 2019 at 13:19
  • C# with Newtonsoft Json library Commented Mar 13, 2019 at 13:41
  • (we want to reduce this time) -- what is taking the time? Your question doesn't explain this at all so we can only guess what your problem is. The JSON shown in the question should be very quick to parse. Commented Mar 13, 2019 at 19:21
  • As I mentioned, the object would have many properties (varies between 100 to 1000). My only question is, do we have any other approach apart from the mentioned in the question? Commented Mar 14, 2019 at 4:34

1 Answer 1

2

Depending on your definition of "optimal":

The shortest way to find a property somewhere in the JSON is to parse the JSON to a JObject and then use SelectToken with a recursive descent JsonPath expression:

public static string FindFirst(string json, string propertyName)
{
    return JObject.Parse(json).SelectToken("$.." + propertyName)?.ToString();
}

Fiddle: https://dotnetfiddle.net/JQxu9c

The fastest way that I know of to do the same thing with Json.Net is to use a JsonTextReader:

public static string FindFirst(string json, string propertyName)
{
    using (StringReader sr = new StringReader(json))
    using (JsonReader reader = new JsonTextReader(sr))
    {
        while (reader.Read())
        {
            if (reader.TokenType == JsonToken.PropertyName && 
                reader.Value.ToString() == propertyName)
            {
                return reader.ReadAsString();
            }
        }
        return null;
    }
}

Fiddle: https://dotnetfiddle.net/aR3qVe

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

1 Comment

This is awesome.. Thank you so much... I will try with my object and decide the approach.

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.