0

How can you access a nested json value when the value has no key?

im trying to access the "2019-03-19T22:57:47.972Z" value of this json object:

var json = {"metaData":[{"name":"ACTION_NAME"},{"name":"SENT_RECV_TIME"}],"rows":[["SI_OA_CTPParameters","2019-03-20T06:20:45.704Z"],["SI_OA_CTPParameters","2019-03-21T06:04:08.313Z"],["SI_OA_CTPParameters","2019-03-21T06:01:14.412Z"],["SI_OA_CTPParameters","2019-03-20T06:59:54.875Z"],["SI_OA_CTPParameters","2019-03-20T20:32:50.975Z"],["SI_OA_CloudDataAddress","2019-03-19T22:57:47.972Z"],["SI_OA_CloudDataAddress","2019-03-19T22:56:52.115Z"],["SI_OA_CloudDataAddress","2019-03-19T22:54:28.196Z"] ......

what is can reach rigth now is just json.rows[0], which return:

["SI_OA_CTPParameters","2019-03-21T06:04:08.313Z"]

I tried json.rows[0].[1] but this does not work.

I just need the second value "2019-03-21T06:04:08.313Z", how can I acces it?

  • 3
    `json.rows[0][1]` will give you results. as you can see `rows` is an array with depth `level 2` so you need to access using indexs – Code Maniac Mar 22 '19 at 18:27
  • 1
    [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – adiga Mar 22 '19 at 18:28

2 Answers2

2

You can access nested values using json.rows[0][1], like this:

var json = {"metaData":[{"name":"ACTION_NAME"},{"name":"SENT_RECV_TIME"}],"rows":[["SI_OA_CTPParameters","2019-03-20T06:20:45.704Z"],["SI_OA_CTPParameters","2019-03-21T06:04:08.313Z"],["SI_OA_CTPParameters","2019-03-21T06:01:14.412Z"],["SI_OA_CTPParameters","2019-03-20T06:59:54.875Z"],["SI_OA_CTPParameters","2019-03-20T20:32:50.975Z"],["SI_OA_CloudDataAddress","2019-03-19T22:57:47.972Z"],["SI_OA_CloudDataAddress","2019-03-19T22:56:52.115Z"],["SI_OA_CloudDataAddress","2019-03-19T22:54:28.196Z"]]};

console.log(json.rows[0][1]);
Deiv
  • 3,000
  • 2
  • 18
  • 30
  • thanks, I wasn't aware I can access a json value like this, I was thinking more of : json.rows[0].1 or something like that. – Gio González Mar 22 '19 at 18:44
  • No problem, feel free to select an answer using the checkmark so that this question can be seen as solved – Deiv Mar 22 '19 at 18:48
1

json.rows[0] returns an array. Lets call this array a. You can reference the elements of an array by their index, thus: a[1] will yield your requested element.

However, renaming the array is not convenient, you can simply substitute the original statement back into the a; thus json.rows[0][1] will work.