-3

var data = JSON.parse('{"orders":[{"orderId":654654654,"orderNumber":"5555555","orderKey":"000000000000-0000000000000","orderDate":"2018-01-18T07:17:58.0000000","createDate":"2018-01-18T07:26:29.1600000","modifyDate":"2018-01-18T07:26:30.0100000","paymentDate":"2018-01-18T07:17:59.0000000","shipByDate":null,"orderStatus":"awaiting_shipment","customerId":55555555,"customerUsername":"username","customerEmail":"email@here.com","billTo":{"name":"JOHN DOE","company":null,"street1":null,"street2":null,"street3":null,"city":null,"state":null,"postalCode":null,"country":null,"phone":null,"residential":null,"addressVerified":null},"shipTo":{"name":"JOHN DOE","company":null,"street1":"123 AVE","street2":"LOGISTICS","street3":null,"city":"MIAMI","state":"FL","postalCode":"33166","country":"US","phone":"333-333-3333","residential":false,"addressVerified":"Address validated successfully"},"items":[{"orderItemId":654654654654,"lineItemKey":"654654654654654","sku":"","name":"IBM Series X3850 X5 4x E7-8870 2.4ghz 40-Cores / 128gb / 8x 146gb / M5015","imageUrl":"https://i.ebayimg.com/5654654654F","weight":{"value":1280.00,"units":"ounces","WeightUnits":1},"quantity":1,"unitPrice":1139.99,"taxAmount":0.00,"shippingAmount":null,"warehouseLocation":null,"options":[],"productId":null,"fulfillmentSku":null,"adjustment":false,"upc":null,"createDate":"2018-01-18T07:26:29.16","modifyDate":"2018-01-18T07:26:29.16"}],"orderTotal":1228.28,"amountPaid":1228.28,"taxAmount":0.00,"shippingAmount":88.29,"customerNotes":null,"internalNotes":null,"gift":false,"giftMessage":null,"paymentMethod":"PayPal","requestedShippingService":"FedExHomeDelivery","carrierCode":"fedex","serviceCode":"fedex_home_delivery","packageCode":"package","confirmation":"none","shipDate":null,"holdUntilDate":null,"weight":{"value":1280.00,"units":"ounces","WeightUnits":1},"dimensions":{"units":"inches","length":35.00,"width":23.00,"height":15.00},"insuranceOptions":{"provider":null,"insureShipment":false,"insuredValue":0.0},"internationalOptions":{"contents":null,"customsItems":null,"nonDelivery":null},"advancedOptions":{"warehouseId":65465465,"nonMachinable":false,"saturdayDelivery":false,"containsAlcohol":false,"mergedOrSplit":false,"mergedIds":[],"parentId":null,"storeId":4654654654,"customField1":null,"customField2":null,"customField3":null,"source":null,"billToParty":null,"billToAccount":null,"billToPostalCode":null,"billToCountryCode":null,"billToMyOtherAccount":null},"tagIds":[654654],"userId":null,"externallyFulfilled":false,"externallyFulfilledBy":null,"labelMessages":null}],"total":1,"page":1,"pages":1}');



var orderId = data.orders[0].orderId;

console.log(orderId);

This is the object:

{"orders":[{"orderId":654654654,"orderNumber":"5555555","orderKey":"000000000000-0000000000000","orderDate":"2018-01-18T07:17:58.0000000","createDate":"2018-01-18T07:26:29.1600000","modifyDate":"2018-01-18T07:26:30.0100000","paymentDate":"2018-01-18T07:17:59.0000000","shipByDate":null,"orderStatus":"awaiting_shipment","customerId":55555555,"customerUsername":"username","customerEmail":"email@here.com","billTo":{"name":"JOHN DOE","company":null,"street1":null,"street2":null,"street3":null,"city":null,"state":null,"postalCode":null,"country":null,"phone":null,"residential":null,"addressVerified":null},"shipTo":{"name":"JOHN DOE","company":null,"street1":"123 AVE","street2":"LOGISTICS","street3":null,"city":"MIAMI","state":"FL","postalCode":"33166","country":"US","phone":"333-333-3333","residential":false,"addressVerified":"Address validated successfully"},"items":[{"orderItemId":654654654654,"lineItemKey":"654654654654654","sku":"","name":"IBM Series X3850 X5 4x E7-8870 2.4ghz 40-Cores / 128gb / 8x 146gb / M5015","imageUrl":"https://i.ebayimg.com/5654654654F","weight":{"value":1280.00,"units":"ounces","WeightUnits":1},"quantity":1,"unitPrice":1139.99,"taxAmount":0.00,"shippingAmount":null,"warehouseLocation":null,"options":[],"productId":null,"fulfillmentSku":null,"adjustment":false,"upc":null,"createDate":"2018-01-18T07:26:29.16","modifyDate":"2018-01-18T07:26:29.16"}],"orderTotal":1228.28,"amountPaid":1228.28,"taxAmount":0.00,"shippingAmount":88.29,"customerNotes":null,"internalNotes":null,"gift":false,"giftMessage":null,"paymentMethod":"PayPal","requestedShippingService":"FedExHomeDelivery","carrierCode":"fedex","serviceCode":"fedex_home_delivery","packageCode":"package","confirmation":"none","shipDate":null,"holdUntilDate":null,"weight":{"value":1280.00,"units":"ounces","WeightUnits":1},"dimensions":{"units":"inches","length":35.00,"width":23.00,"height":15.00},"insuranceOptions":{"provider":null,"insureShipment":false,"insuredValue":0.0},"internationalOptions":{"contents":null,"customsItems":null,"nonDelivery":null},"advancedOptions":{"warehouseId":65465465,"nonMachinable":false,"saturdayDelivery":false,"containsAlcohol":false,"mergedOrSplit":false,"mergedIds":[],"parentId":null,"storeId":4654654654,"customField1":null,"customField2":null,"customField3":null,"source":null,"billToParty":null,"billToAccount":null,"billToPostalCode":null,"billToCountryCode":null,"billToMyOtherAccount":null},"tagIds":[654654],"userId":null,"externallyFulfilled":false,"externallyFulfilledBy":null,"labelMessages":null}],"total":1,"page":1,"pages":1}

To get the orderId value in my returned json object I use: (data.order) is the object.

data.order[0].orders.orderId 

I just get not returned value or undefined on anything I try. If I output the data.order object I obviously get the whole JSON object. I've read through several threads here and can seem to find a object/array as complex as the one I'm working on. Any help is appreciated.

I removed the return array to dumb it down. Here is my function in case you were curious. Any if you don't understand some odd markup I'm using the Symfony framework and Twig.

function getOrderInfo( orderId, location ){
    var data = {
        id : orderId
    };
    $.ajax({
            type: "POST",
            url: "{{ path('Dashboard_OrderTracker_orderInfo') }}",
            data: data
        })
        .done(function (data) {
            var jsondata = JSON.parse(data);
            var orderNumber = jsondata.orders[0].orderNumber;
            var name = jsondata.orders[0].shipTo.name;
            var phone = jsondata.orders[0].shipTo.phone;
            var customerEmail = jsondata.orders[0].customerEmail;

            $('#info').html( 
                    "<b>Order number:</b> " + orderNumber + "<br/>" +
                    "<b>Customer Name:</b> " + name + "<br/>" +
                    "<b>Cutomer Phone:</b> " + phone + "<br/>" +
                    "<b>Customer Email:</b> <a href='mailto:" + customerEmail + ">" + customerEmail + "</a><br/>"                       
            );
            $( "#info" ).dialog("option", "position", {
                of: location
            }).dialog("open");
        })
        .fail(function ( xhr.responseText ) {
            alert(xhr);
        });

};
CalvT
  • 3,123
  • 6
  • 37
  • 54
KungFuMonkey
  • 91
  • 1
  • 3
  • Where is `data.order` defined? – guest271314 Jan 19 '18 at 00:41
  • 1
    there is no property called `order` - assuming `data` is what holds this **javascript object** *(because JSON is a string, which when parsed becomes an object, and no longer JSON, there's no such thing in javascript as a JSON object)*, then `data.orders[0].orderId` seems to be what you want – Jaromanda X Jan 19 '18 at 00:41
  • I am returning multiple responses on the ajax call, using an array. Like so: return new JsonResponse(array('order' => $orders, id => $id), 200); To get the order part of the array I access data.order. I'm usung PHP on the backend. – KungFuMonkey Jan 19 '18 at 00:45
  • I had to use JSON.parse() to get the data needed. Thanks for all the help.... – KungFuMonkey Jan 19 '18 at 01:22
  • 5
    Please do not vandalize your posts. Once you've posted a question, you have licensed the content to the Stack Overflow community at large (under the CC-by-SA license). – CalvT Jan 19 '18 at 17:45
  • I tried to delete the supposed "DUPLICATE" post as suggested in the help and there wasn't a delete option. – KungFuMonkey Jan 26 '18 at 20:17

1 Answers1

0

Assuming data is your object variable... The proper way to access the orderId is:

data.orders[0].orderId

I suggest prettifying your JSON object, so that you can better see this.

wnamen
  • 550
  • 3
  • 12