How do I check if a JSON element Exists?

I have a deserialized JSON string:

{
    "status": "In Transit",
    "proNumber": "0415158983",
    "estimatedServiceTime": {
        "estDate": "2022-09-07",
        "estTime": "By 5:00 PM",
        "message": "This is only an estimate based upon standard service times."
    },
    "pickupInformation": {
        "shipperName": "SAFELITE 241",
        "dateTime": "2022-09-02T16:33:00",
        "originPoint": "BRASELTON,GA"
    },
    "deliveryInformation": {
        "consigneeName": "SAFELITE",
        "deliveryPoint": "SAINT LOUIS,MO",
        "serviceCenter": "ST LOUIS SERVICE CENTER"
    },
    "shipmentInformation": {
        "pieces": "2",
        "weight": "210.0",
        "bolNumber": "R4106111",
        "poNumbers": [
            "NS"
        ],
        "hazmat": "No"
    },
    "billToInformation": {
        "billTo": "SAFELITE GROUP",
        "address1": "RUAN TRANSPORTATION",
        "address2": "PO BOX 9319",
        "city": "DES MOINES",
        "state": "IA",
        "zip": "50306"
    },
    "commerceInformation": {
        "terms": "Prepaid (charge)",
        "grossCharges": "841.55",
        "discount": "538.59",
        "fuelSurcharge": "113.61",
        "netCharges": "416.57"
    },
    "shipmentHistory": [
        {
            "location": "LAWRENCEVILLE,GA",
            "dateTime": "2022-09-02T18:57:00",
            "status": "ORIGIN DEPARTURE"
        },
        {
            "location": "NASHVILLE,TN",
            "dateTime": "2022-09-02T23:15:00",
            "status": "RELAY ARRIVAL"
        }
    ]
}

From which I grab the shipmentInformation array information. But sometimes I will get a JSON string that contains no shipmentInformation element, such as

{
    "status": "Out for Delivery",
    "proNumber": "0061983505",
    "estimatedServiceTime": {
        "estDate": "2022-09-01",
        "estTime": "By 5:00 PM",
        "message": "This is only an estimate based upon standard service times."
    },
    "pickupInformation": {
        "shipperName": "SAFELITE",
        "dateTime": "2022-08-31T11:03:00",
        "originPoint": "SOUTHAVEN,MS"
    },
    "deliveryInformation": {
        "consigneeName": "SAFELITE GLASS CORPORATION",
        "deliveryPoint": "PEARL,MS",
        "serviceCenter": "JACKSON MS SERVICE CTR"
    },
    "billToInformation": {
        "billTo": "SAFELITE GROUP",
        "address1": "RUAN TRANSPORTATION",
        "address2": "PO BOX 9319",
        "city": "DES MOINES",
        "state": "IA",
        "zip": "50306"
    },
    "commerceInformation": {
        "terms": "Prepaid (charge)",
        "grossCharges": "283.36",
        "discount": "155.85",
        "fuelSurcharge": "47.82",
        "netCharges": "175.33"
    }
}

Which causes the workflow to error out. Is there a way to check if a JSON element exists within a flow?

Hello @David_Hernandez ,

Assuming you have deserialized your JSON using Deserialize JSON activity and now you have a JsonObject let’s call it myJsonObject you should be able to tell if a given key exists using this:

myJsonObject.ContainsKey("shipmentInformation")

the function ContainsKey returns a boolean object that you can easily compare with an if statment

hope this helps :robot:

3 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.