0

I´m new on JSON /JAVASCRIPT. This is whhat i have

JSON OBJECT

const object = {
    "id": "123412341243",
    "intent": "CAPTURE",
    "purchase_units": [
        {
            "reference_id": "PU1",
            "amount": {
                "currency_code": "EUR",
                "value": "18.98",
                "breakdown": {
                    "item_total": {
                        "currency_code": "EUR",
                        "value": "18.98"
                    },
                    "shipping": {
                        "currency_code": "EUR",
                        "value": "0.00"
                    },
                    "handling": {
                        "currency_code": "EUR",
                        "value": "0.00"
                    },
                    "tax_total": {
                        "currency_code": "EUR",
                        "value": "0.00"
                    },
                    "insurance": {
                        "currency_code": "EUR",
                        "value": "0.00"
                    },
                    "shipping_discount": {
                        "currency_code": "EUR",
                        "value": "0.00"
                    }
                }
            },
            "payee": {
                "email_address": "c...r@gmail.com",
                "merchant_id": "848UWWSMK2TC4"
            },
            "description": "InfoPosit",
            "custom_id": "1231231231",
            "invoice_id": "Iabcabc",
            "items": [
                {
                    "name": "PRODUCTO 1",
                    "unit_amount": {
                        "currency_code": "EUR",
                        "value": "8.99"
                    },
                    "quantity": "1",
                    "sku": "fi78PSRA"
                },
                {
                    "name": "PRODUCTO 2",
                    "unit_amount": {
                        "currency_code": "EUR",
                        "value": "9.99"
                    },
                    "quantity": "1",
                    "sku": "fi11PMSRA"
                },
                {
                    "name": "PRODUCTO 3",
                    "unit_amount": {
                        "currency_code": "EUR",
                        "value": "9.99"
                    },
                    "quantity": "1",
                    "sku": "fi11PMSRA"
                }
            ],
            "shipping": {
                "name": {
                    "full_name": "test buyer"
                },
                "address": {
                    "address_line_1": "15 Mary Street",
                    "admin_area_2": "North Sydney",
                    "admin_area_1": "NSW",
                    "postal_code": "2001",
                    "country_code": "AU"
                }
            }
        }
    ],
    "payer": {
        "name": {
            "given_name": "test",
            "surname": "buyer"
        },
        "email_address": "c......r@gmail.com",
        "payer_id": "AAAAAAAAAA",
        "address": {
            "country_code": "ES"
        }
    },
    "create_time": "2020-05-10T00:36:33Z",
    "links": [
        {
            "href": "https://api.sandbox.paypal.com/v2/checkout/orders/21312312",
            "rel": "self",
            "method": "GET"
        },
        {
            "href": "https://api.sandbox.paypal.com/v2/checkout/orders/123123123",
            "rel": "update",
            "method": "PATCH"
        },
        {
            "href": "https://api.sandbox.paypal.com/v2/checkout/orders/123123123/capture",
            "rel": "capture",
            "method": "POST"
        }
    ],
    "status": "APPROVED"
}

Whith this code i get the NAME value from ITEMS... but i dont know how to put each value into a INPUT.

i´m trying to Get all names (purchase_units.items.name) and values (purchase_units.items.unit_amount.value) from my obbject and insert them each one in a input HTML.

JAVSCRIPT file

var key =new Array();
var key1= object.purchase_units[0].items;
for(var i=0, len=key1.length; i<len; i++) {

   key[i] = object.purchase_units[0].items[i].name;

}
 console.log(key);

i´ve tried searchin on enternet .. but after some days of searching i tough that some one here can help me..

  • Provide your HTML as well – Ehsan Mahmud May 10 '20 at 17:24
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder May 10 '20 at 17:24
  • The answers to [this question](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) show you how to access the data within the object. The answers to [this question](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) show you how to loop through an array (like `purchase_units`). The answers to [this question](https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object) show you how to loop through the properties of an object (such as `breakdown`). – T.J. Crowder May 10 '20 at 17:27

0 Answers0