Home > Blockchain >  JObject SelectToken Unexpected character while parsing path indexer
JObject SelectToken Unexpected character while parsing path indexer

Time:10-08

I need to parse an API json response to pull out a certain value. This method has always worked well for me without issue until tonight. When attempting to execute this code:

JObject oR = JObject.Parse(response2.Content);                   

if (oR.SelectToken("[response][0][assignment][current][channelId]") != null)
{
    ChannelId = oR.SelectToken("[response][0][assignment][current][channelId]").ToString();
}

I receive this error:

Unexpected character while parsing path indexer: r

Here is the full json response from the api I'm attempting to parse:

{
    "response": [
        {
            "id": 268633,
            "parentOrderId": 0,
            "orderTypeCode": "SO",
            "reference": "#351569",
            "version": 3,
            "state": {
                "tax": "READY"
            },
            "orderStatus": {
                "orderStatusId": 41,
                "name": "Ready to Ship"
            },
            "tax": {
                "errors": []
            },
            "orderPaymentStatus": "PAID",
            "stockStatusCode": "SOA",
            "allocationStatusCode": "AAA",
            "shippingStatusCode": "SNS",
            "placedOn": "2021-10-07T18:53:54.000-05:00",
            "createdOn": "2021-10-07T18:54:12.000-05:00",
            "updatedOn": "2021-10-07T18:57:43.000-05:00",
            "createdById": 4,
            "priceListId": 3,
            "priceModeCode": "EXC",
            "delivery": {
                "deliveryDate": "2021-10-06T19:00:00.000-05:00",
                "shippingMethodId": 9
            },
            "invoices": [
                {
                    "invoiceReference": "",
                    "taxDate": "2021-10-07T00:00:00.000-05:00",
                    "dueDate": "2021-10-06T19:00:00.000-05:00"
                }
            ],
            "currency": {
                "accountingCurrencyCode": "USD",
                "orderCurrencyCode": "USD",
                "exchangeRate": "1.000000",
                "fixedExchangeRate": true
            },
            "totalValue": {
                "net": "14.10",
                "taxAmount": "1.16",
                "baseNet": "14.10",
                "baseTaxAmount": "1.16",
                "baseTotal": "15.26",
                "total": "15.26"
            },
            "assignment": {
                "current": {
                    "staffOwnerContactId": 0,
                    "projectId": 0,
                    "channelId": 3,
                    "leadSourceId": 0,
                    "teamId": 0
                }
            },
            "parties": {
                "customer": {
                    "contactId": 748240,
                    "addressFullName": "John Doe",
                    "companyName": "",
                    "addressLine1": "225 N First ST",
                    "addressLine2": "",
                    "addressLine3": "My Town",
                    "addressLine4": "Texas",
                    "postalCode": "76082-2583",
                    "country": "United States",
                    "telephone": " 1900MIXALOT",
                    "mobileTelephone": "",
                    "fax": "",
                    "email": "[email protected]",
                    "countryId": 223,
                    "countryIsoCode": "US",
                    "countryIsoCode3": "USA"
                },
                "delivery": {
                    "addressFullName": "John Doe",
                    "companyName": "",
                    "addressLine1": "225 N First ST",
                    "addressLine2": "",
                    "addressLine3": "My Town",
                    "addressLine4": "Texas",
                    "postalCode": "76082-2583",
                    "country": "United States",
                    "telephone": " 1900MIXALOT",
                    "mobileTelephone": "",
                    "fax": "",
                    "email": "[email protected]",
                    "countryId": 223,
                    "countryIsoCode": "US",
                    "countryIsoCode3": "USA"
                },
                "billing": {
                    "contactId": 748240,
                    "addressFullName": "John Doe",
                    "companyName": "",
                    "addressLine1": "225 N First ST",
                    "addressLine2": "",
                    "addressLine3": "My Town",
                    "addressLine4": "Texas",
                    "postalCode": "76082-2583",
                    "country": "United States",
                    "telephone": " 1900MIXALOT",
                    "mobileTelephone": "",
                    "fax": "",
                    "email": "[email protected]",
                    "countryId": 223,
                    "countryIsoCode": "US",
                    "countryIsoCode3": "USA"
                }
            },
            "orderRows": {
                "600995": {
                    "orderRowSequence": "10",
                    "productId": 30727,
                    "productName": "Mens Wrangler Sport Western Plaid Snap - M",
                    "productSku": "395802",
                    "quantity": {
                        "magnitude": "1.0000"
                    },
                    "itemCost": {
                        "currencyCode": "USD",
                        "value": "11.2500"
                    },
                    "productPrice": {
                        "currencyCode": "USD",
                        "value": "24.9900"
                    },
                    "discountPercentage": "0.00",
                    "rowValue": {
                        "taxRate": "9.5000",
                        "taxCode": "T",
                        "taxCalculator": "manual",
                        "rowNet": {
                            "currencyCode": "USD",
                            "value": "4.9900"
                        },
                        "rowTax": {
                            "currencyCode": "USD",
                            "value": "0.4000"
                        },
                        "taxClassId": 2
                    },
                    "productOptions": {
                        "Top Size": "M",
                        "Color": "Multi"
                    },
                    "nominalCode": "4000",
                    "composition": {
                        "bundleParent": false,
                        "bundleChild": false,
                        "parentOrderRowId": 0
                    },
                    "externalRef": "10197546795147",
                    "clonedFromId": 0
                },
                "600997": {
                    "orderRowSequence": "30",
                    "productId": 1001,
                    "productName": "Shipping: UPS SurePost",
                    "productSku": "",
                    "quantity": {
                        "magnitude": "1.0000"
                    },
                    "itemCost": {
                        "currencyCode": "USD",
                        "value": "0.0000"
                    },
                    "productPrice": {
                        "currencyCode": "USD",
                        "value": "9.1100"
                    },
                    "discountPercentage": "0.00",
                    "rowValue": {
                        "taxRate": "9.5000",
                        "taxCode": "T",
                        "taxCalculator": "manual",
                        "rowNet": {
                            "currencyCode": "USD",
                            "value": "9.1100"
                        },
                        "rowTax": {
                            "currencyCode": "USD",
                            "value": "0.7600"
                        },
                        "taxClassId": 2
                    },
                    "nominalCode": "4030",
                    "composition": {
                        "bundleParent": false,
                        "bundleChild": false,
                        "parentOrderRowId": 0
                    },
                    "clonedFromId": 0
                },
                "600996": {
                    "orderRowSequence": "20",
                    "productId": 1000,
                    "productName": "Coupon: PDJA046209",
                    "productSku": "",
                    "quantity": {
                        "magnitude": "1.0000"
                    },
                    "itemCost": {
                        "currencyCode": "USD",
                        "value": "0.0000"
                    },
                    "productPrice": {
                        "currencyCode": "USD",
                        "value": "0.0000"
                    },
                    "discountPercentage": "0.00",
                    "rowValue": {
                        "taxRate": "0.0000",
                        "taxCode": "-",
                        "taxCalculator": "brightpearl",
                        "rowNet": {
                            "currencyCode": "USD",
                            "value": "0.0000"
                        },
                        "rowTax": {
                            "currencyCode": "USD",
                            "value": "0.00"
                        },
                        "taxClassId": 1
                    },
                    "productOptions": {
                        "Top Size": "S",
                        "Color": "Green"
                    },
                    "nominalCode": "4020",
                    "composition": {
                        "bundleParent": false,
                        "bundleChild": false,
                        "parentOrderRowId": 0
                    },
                    "clonedFromId": 0
                }
            },
            "warehouseId": 3,
            "acknowledged": 0,
            "costPriceListId": 1,
            "historicalOrder": false,
            "externalRef": "3912489140363",
            "installedIntegrationInstanceId": 484,
            "orderWeighting": 100
        }
    ]
}

Unfortunately, I have no control over the json that is returned from the webservice API. The best I have been able to dig up is that there is an issue with one of the json keys, but I can't seem to figure out how to fix it or account for it.

Thanks in advance for any help.

CodePudding user response:

The correct JSONPath syntax is:

response[0].assignment.current.channelId

...or use enquoted property names:

['response'][0]['assignment']['current']['channelId']

Personally I prefer the former syntax as it's much cleaner and easier to read.


Additionally, your program is also inefficient because you're evaluating the JSONPath twice. Instead use C#'s is var operator to store the result:

JObject oR = JObject.Parse(response2.Content);                   

if (oR.SelectToken("response[0].assignment.current.channelId") is JToken channelIdToken)
{
    this.ChannelId = channelIdToken.ToString();
}
  • Related