Home > Back-end >  Selector For JSON Response Content
Selector For JSON Response Content

Time:09-21

I can't seem to find the right selector for this json result.

{
    "response": {
        "1017": {
            "total": {
                "inStock": 1,
                "onHand": 1,
                "allocated": 0,
                "inTransit": 0
            },
            "warehouses": {
                "3": {
                    "byLocation": {
                        "6": {
                            "inStock": 1,
                            "onHand": 1,
                            "allocated": 0,
                            "inTransit": 0
                        }
                    },
                    "inStock": 1,
                    "onHand": 1,
                    "allocated": 0,
                    "inTransit": 0
                }
            }
        }
    }
}

What I'm trying to get to is the byLocation > 6 > onHand value.

I thought this would work, but does not return a value:

JObject o = JObject.Parse(response.Content);

QOH = o.SelectToken("response[0].1017[0].warehouses[0].3[0].byLocation[0].6[0].onHand").ToString();

Any help is greatly appreciated.

CodePudding user response:

try this

var QOH = o.SelectToken("response.1017.warehouses.3.byLocation.6.onHand").ToString(); 

result

1

CodePudding user response:

You can querying JSON with SelectToken also without specifying the full path

o.SelectToken("$..byLocation..onHand")?.ToString()

o.SelectToken("$..byLocation.*.onHand")?.ToString()

o.SelectToken("$..byLocation.6.onHand")?.ToString()

o.SelectToken("$..warehouses.*.byLocation.*.onHand")?.ToString()
  • Related