How to store part of JSON response in a variable if criteria

Hi!

Very new to APIs and Postman. I’m using the app and haven’t figured out how to run from terminal just yet.

I’m trying to run a collection of requests where the first request is a GET and the second request is a POST.

I need to save part of the GET response (JSON) if criteria is met. Here is the GET response:

{
    "fulfillment_orders": [
        {
            **"id": 6047600705718,**
            "shop_id":,
            "order_id":,
            "assigned_location_id": 67016458422,
            "request_status": "unsubmitted",
            "status": "open",
            "supported_actions": [
                "create_fulfillment",
                "move",
                "hold"
            ],
            "destination": {
            },
            "line_items": [
                {
                    "id": 12762842955958,
                    "shop_id":,
                    "fulfillment_order_id": 6047600705718,
                    "quantity": 0,
                    "line_item_id": 12599283417270,
                    "inventory_item_id": 44438930358454,
                    "fulfillable_quantity": 0,
                    **"variant_id": 42352914890934**
                },
                {
                    "id": 12762842988726,
                    "shop_id":,
                    "fulfillment_order_id": 6047600705718,
                    "quantity": 1,
                    "line_item_id": 12599283450038,
                    "inventory_item_id": 44442032046262,
                    "fulfillable_quantity": 1,
                    **"variant_id": 42356009107638**
                }
            ],
            "fulfill_at": "2023-08-27T06:00:00-04:00",
            "fulfill_by": null,
            "international_duties": {
                "incoterm": null
            },
            "fulfillment_holds": [],
            "delivery_method": {
                "id": 595030343862,
                "method_type": "shipping",
                "min_delivery_date_time": null,
                "max_delivery_date_time": null
            },
            "created_at": "2023-08-27T06:13:39-04:00",
            "updated_at": "2023-08-27T06:13:39-04:00",
            "assigned_location": {
            },
            "merchant_requests": []
        },
        {
            **"id": 6070784295094,**
            "shop_id":,
            "order_id": 5024240402614,
            "assigned_location_id": 68198531254,
            "request_status": "unsubmitted",
            "status": "open",
            "supported_actions": [
                "create_fulfillment",
                "move",
                "hold"
            ],
            "destination": {
            },
            "line_items": [
                {
                    "id": 12809295921334,
                    "shop_id": ,
                    "fulfillment_order_id": 6070784295094,
                    "quantity": 1,
                    "line_item_id": 12599283417270,
                    "inventory_item_id": 44438930358454,
                    "fulfillable_quantity": 1,
                    **"variant_id": 42352914890934**
                }
            ],
            "fulfill_at": "2023-08-27T06:00:00-04:00",
            "fulfill_by": null,
            "international_duties": {
                "incoterm": null
            },
            "fulfillment_holds": [],
            "delivery_method": {
                "id": 612141924534,
                "method_type": "shipping",
                "min_delivery_date_time": null,
                "max_delivery_date_time": null
            },
            "created_at": "2023-09-14T20:51:23-04:00",
            "updated_at": "2023-09-14T20:51:23-04:00",
            "assigned_location": {
            },
            "merchant_requests": []
        }
    ]
}

I only want the ID number (in **s above) saved as a variable if the “line_items” “variant_id” (also in bold) is any of the following numbers:

42356009140406
42356009173174
42356009107638
42356009205942
42356009238710

In the example response above, there are two IDs, but only one meets the criteria (6047600705718, the second ID 6070784295094 does not meet the criteria). I’d want that ID to be put in a variable called {{fulfillment_order_id}} which is used in the next request in the collection.

Appreciate any help!

Can you please clarify.

In your example response, you have two orders.

The first order has two line items with variant ID’s.

The second order has one line item with a corresponding variant ID.

You want to store the main order ID for the first order that has a variant ID within your allowed list? Is that correct.

Is it possible for both orders to pass the criteria? What happens then? Or are you only interested in the first order that meets the criteria?

Consider the following code…

const response = pm.response.json()

// step 1 - defined allowed variant Ids

let allowedVariantIds = [
    42356009140406,
    42356009173174,
    42356009107638,
    42356009205942,
    42356009238710
]

// step 2 - there are multiple orders in response, so we need a loop
response.fulfillment_orders.forEach(order => {
    // console.log(order);
    // step 3 - map all of the varient Ids in the order line items
    let variantIDs = order.line_items.map(line_items => line_items.variant_id)
    // console.log(variantIDs);
    // step 4 - check if the variant ID array contains any element in the allowed variants array
    let found = variantIDs.some(r=> allowedVariantIds.includes(r))  // either true or false
    // console.log(found);
    if (found === true) { // step 5 - if found then set collection variable
        pm.collectionVariables.set("fulfillment_order_id", order.id)
        console.log(pm.collectionVariables.get("fulfillment_order_id")); // 6047600705718
    }
});

This will go through both orders, so if both orders meet the criteria, then it will be the last order that meets the criteria that is set as the collection variable.

This worked like a CHARM! And so helpful learning. Really appreciate it! There is a chance there would be multiple orders and each order might have multiple products (VariantIds)… but there should only be one order that has the allowed variant IDs. Again… thanks so much!

1 Like

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