user1919
user1919

Reputation: 3938

Access elements in JSON after ajax request

I have spent the last couple of hours trying to figure out this. I make an ajax request using JQuery. I get a response in string format and I use

jQuery.parseJSON(response);

to convert it to Object. This is my response:

{
    "columns": ["n"],
    "data": [
        [{
            "extensions": {},
            "labels": "http://localhost:7474/db/data/node/168/labels",
            "outgoing_relationships": "http://localhost:7474/db/data/node/168/relationships/out",
            "traverse": "http://localhost:7474/db/data/node/168/traverse/{returnType}",
            "all_typed_relationships": "http://localhost:7474/db/data/node/168/relationships/all/{-list|&|types}",
            "property": "http://localhost:7474/db/data/node/168/properties/{key}",
            "self": "http://localhost:7474/db/data/node/168",
            "properties": "http://localhost:7474/db/data/node/168/properties",
            "outgoing_typed_relationships": "http://localhost:7474/db/data/node/168/relationships/out/{-list|&|types}",
            "incoming_relationships": "http://localhost:7474/db/data/node/168/relationships/in",
            "create_relationship": "http://localhost:7474/db/data/node/168/relationships",
            "paged_traverse": "http://localhost:7474/db/data/node/168/paged/traverse/{returnType}{?pageSize,leaseTime}",
            "all_relationships": "http://localhost:7474/db/data/node/168/relationships/all",
            "incoming_typed_relationships": "http://localhost:7474/db/data/node/168/relationships/in/{-list|&|types}",
            "metadata": {
                "id": 168,
                "labels": []
            },
            "data": {
                "name": "1"
            }
        }]
    ]
}

I try to access specific elemets of this object but I get nothing back. When I try this

var test = json.data;

it works but how can I access the values saved in metadata. I try this but I get "undefined":

var test = json.data.metadata.id;

Any idea what I am missing?

Upvotes: 3

Views: 1595

Answers (3)

Amit Joki
Amit Joki

Reputation: 59232

It is an array, so you've to use index

var test = json.data[0][0].metadata.id;

json.data[0] returns [{...}] and again [{...}][0] returns {...} which has metadata Object, which in turn as id property which is what you need.

Upvotes: 1

A. Rama
A. Rama

Reputation: 912

json.data is a double array.

This should work:

var test = json.data[0][0].metadata.id;

Upvotes: 1

chead23
chead23

Reputation: 1879

Try:

var test = json.data[0][0].metadata.id;

json.data is an array of arrays.

Upvotes: 0

Related Questions