0

I have complex anonymous JSON string.

For example:

{ someData: { test1: { test2: { test4: "data" }, test3: "my text" } } }

And I need to find one property in this JSON. So, for example, if JSON contains someData.test1.test3 property, then I need to use value of this property my text. Is there a simple way to do (handle any level of nesting) that?

A. Gladkiy
  • 3,134
  • 5
  • 38
  • 82

1 Answers1

1

You can use newtonsoft.json to parse this json to a JToken using

JToken token = JToken.Parse("{ \"someData\": { \"test1\": { \"test2\": { \"test4\": \"data\" }, \"test3\": \"my text\" } } }");

Then using SelectToken you can evaluate to a path and get the value you wish.

JToken result = token.SelectToken("$.someData.test1.test3");

Hope this helps!

See on DotNetFiddle: https://dotnetfiddle.net/7cyRzv

  • Great! Thanks a lot, but what if I need case-insensitive check? – A. Gladkiy Apr 23 '20 at 11:02
  • 1
    There is not a build in functionality for JsonPath to do case insensitive pathing. You'll have to code something on your own to run over the path and do case insensetive navigation. This question has been asked before: https://stackoverflow.com/questions/35704390/possible-to-search-case-insensitive-with-jsonpath @A.Gladkiy – Davey van Tilburg Apr 23 '20 at 11:07