Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 288
user1919 Avatar asked Apr 25 '26 17:04

user1919


1 Answers

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.

like image 55
Amit Joki Avatar answered Apr 28 '26 08:04

Amit Joki