1

I am pretty new to coding. I am trying to define and store data from an API, but can't manage to define the values that exists nested in another object. Been looking around for a solution but the examples doesn't really work for me.

This is a part of the code:

for (var i in json) {

    var TestOrdersXML = <testOrders _key="@testOrderId" operation="insertOrUpdate"/>;

    if (json[i].testOrderId != undefined) TestOrdersXML.@testOrderId = json[i].testOrderId;
    if (json[i].customerId != undefined) TestOrdersXML.@customerId = json[i].customerId;
    if (json[i].installationOrderData.state != undefined) TestOrdersXML.@state = json[i].installationOrderData.state;


   logInfo(" Test order Id: " + json[i].testOrderId + " Customer Id: " + json[i].customerId + " STATE: " + json[i].state);
    //collection.appendChild(TestOrdersXML);
  }

The response I get from the log is: Test order Id: 123456 Customer Id: 123456 STATE: undefined.

This is the json I am trying to get:

[
    {
        "installationOrderData":{
            "state": "booked"
        },
        "customerId": 123456,
        "testOrderId": 123456
    }
]

Thanks.

2
  • By json[i].state, do you mean json[i].installationOrderData.state? Commented Dec 5, 2018 at 14:01
  • Yes, that is correct... Oh I see now .. haha thanks Commented Dec 5, 2018 at 14:02

1 Answer 1

2

You are logging " " STATE: " + json[i].state" instead of " " STATE: " + json[i].installationOrderData.state " .

Sign up to request clarification or add additional context in comments.

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.