0

I am trying to get a value from the nested JSON data below. Specifically, the payments captures id value 0TA12948FV40723B is the value I need.

I try using the following codes to retrieve the value.

details.purchase_units.payments.captures.id

details.purchase_units.payments[0].captures.id

But I keep getting a console.log Error: "Order could not be captured"

JSON DATA

{
    "create_time":"2019-02-19T05:06:52Z",
    "update_time":"2019-02-19T05:06:52Z",
    "id":"3HB96413YD922272B",
    "intent":"CAPTURE",
    "status":"COMPLETED",
    "payer":{
        "email_address":"[email protected]",
        "payer_id":"WEJUPTK4U53E9",
        "address":{
            "address_line_1":"1 Main St",
            "admin_area_2":"San Jose",
            "admin_area_1":"CA",
            "postal_code":"95131",
            "country_code":"US"
        },
        "name":{
            "given_name":"a",
            "surname":"som"
        },
        "phone":{
            "phone_number":{
                "national_number":"408-214-8270"
            }
        }
    },
    "purchase_units":[{
        "reference_id":"default",
        "amount":{
            "value":"1.01",
            "currency_code":"USD"
        },
        "payee":{
            "email_address":"[email protected]",
            "merchant_id":"MSOIGVMKKWAMA"
        },
        "shipping":{
            "name":{
                "full_name":"Mr T"
            },
            "address":{
                "address_line_1":"1234 Main St.",
                "address_line_2":"Unit 1",
                "admin_area_2":"Chicago",
                "admin_area_1":"IL",
                "postal_code":"60652","country_code":"US"
            }
        },
        "payments":{
            "captures":[{
                "status":"COMPLETED",
                "id":"0TA12948FV40723B",
                "final_capture":true,
                "create_time":"2019-02-20T05:06:52Z",
                "update_time":"2019-02-20T05:06:52Z",
                "amount":{
                    "value":"1.01","currency_code":"USD"
                },
            "seller_protection":{
                "status":"ELIGIBLE",
                "dispute_categories":[
                    "ITEM_NOT_RECEIVED",
                    "UNAUTHORIZED_TRANSACTION"
                ]}
            }
        ]}
    }]
}

4 Answers 4

1

Since purchase_units and captures are arrays:

details.purchase_units[0].payments.captures[0].id
Sign up to request clarification or add additional context in comments.

Comments

0

captures key have objects in side an array. so you can't simply fetch like

details.purchase_units.payments[0].captures.id

you have to give the position of the captures or loop the array only you can do parsing

details.purchase_units.payments[0].captures[0].id

Comments

0

If you add your json as object using the chrome console you will be able to do what I did in in the image below. purchase_unit is an array so you need to access it using the index.

javascript console

Comments

0

In case if you want to capture all the IDs in an array, you can use wild card:

details.purchase_units[*].payments.captures[*].id

This will parse complete payload for specified path and collect all the IDs in an array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.