-1

I cannot serialize the data to C# object as the structure of JSON is not the same for same set of objects. Is there some kind of function that lets you search through all JSON properties and their child properties in order to return a value of the property you searched for?

Examples of the data I'm receiving through API (I'm trying to return the value of property "propertyIamLookingFor"):

First Example

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "propertyIamLookingFor": 500,
        "height": 500
    },
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    }
}} 

Second Example

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "height": 500
    },
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    },
    "propertyIamLookingFor": 32
}}

Third Example

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "height": 500
    },
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "window": {
            "title": "Sample Konfabulator Widget",
            "name": "main_window",
            "height": 500,
            "propertyIamLookingFor": 11
        }
    }
}} 

Notice that the property always has the same name regardless of its position, so I know the name of the property and I'm trying to return the value.

*In case anyone wonders, it is a bad API I'm accessing, but have no option to not working with it.

Alen Šimunic
  • 555
  • 1
  • 7
  • 19
  • Have you looked at JsonPath? https://www.newtonsoft.com/json/help/html/QueryJsonSelectTokenJsonPath.htm – Charleh Feb 14 '18 at 13:18
  • That function assumes that you are familiar with the value, which I am not, it can literally be anything as they are messages which I'm trying to read. I'm looking for a function in which you can input property name and get back the value for that property, but regardless of the property position in the JSON structure, as it's position is completely variable. – Alen Šimunic Feb 14 '18 at 13:20
  • Without an example of the type of data you're receiving, I'd be shooting in the dark trying to answer you here. – Danielle Summers Feb 14 '18 at 13:24
  • @DanielleSummers here are examples – Alen Šimunic Feb 14 '18 at 13:29
  • So what is the rule to find that property? Name? You can [enumerate json](https://stackoverflow.com/q/10543512/1997232) and find it. – Sinatr Feb 14 '18 at 13:36
  • I tried enumeration, but it only enumerates the first level of the structure and doesn't go into the child nodes to look for the property – Alen Šimunic Feb 14 '18 at 13:38
  • Did you even read about JSONPath? Pretty sure like xpath you can select multiple tokens by name regardless of their position in the json – Charleh Feb 14 '18 at 14:30
  • Tbh, I'm not sure why people downvoted and marked this question as "too broad". It's a decent enough question with a very particular answer...! – Charleh Feb 15 '18 at 13:55

2 Answers2

1

You can use JSONPath for this, it's similar to XPath.

For example to find all properties called myProp use the JSONPath expression

$..myProp

This is a recursive selector that will iterate all properties and child props etc.

JSONPath is built into the json.net library

Take another look at http://newtonsoft.com/json/help/html/QueryJsonSelectTokenJsonPath.htm but dig deeper :)

Charleh
  • 13,749
  • 3
  • 37
  • 57
0

I didn't manage to find a function in JSON.Net to find a property (regardless of position in JSON structure) and return its value, but XmlDocument.GetElementsByTagName works pretty well with XML. So I simply converted JSON to XML with JsonConvert.DeserializeXmlNode and then used XmlDocument.GetElementsByTagName to find my property. Hope it helps everyone else with the same issue and hopefully someone else posts an easier way to achieve this.

EDIT: Charleh managed to point out the way in JSON, accepted as the best answer

Alen Šimunic
  • 555
  • 1
  • 7
  • 19
  • Use jsonpath like I said before. Go here http://jsonpath.com and change the search expression to `$..type` for an example – Charleh Feb 14 '18 at 14:34