Accessing element in REST export view json response

by John Rey Tanquinco   Last Updated May 28, 2018 08:07 AM

I have an API created from a REST export view, now im trying to do some behat test, but im getting some strange/different result when accessing an element in the json formatted response.

Using this method to evaluate if the expression is valid/invalid:

public function evaluate(Json $json, $expression)
{
    if ($this->evaluationMode === 'javascript') {
        $expression = str_replace('->', '.', $expression);
    }
    try {
        return $json->read($expression, $this->propertyAccessor);
    } catch (\Exception $e) {
        throw new \Exception(sprintf('Failed to evaluate expression "%s"', $expression), 0, $e);
    }
}

This is the output of a regular node/content when I access node/1/?_format=json and I was able to get the nid value by typing nid[0].value. This is enclosed in a curly braces and I believe its an object.

{
    "nid": [
        {
            "value": 1
        }
    ],
    ...
}

However, in my REST export view(path = api/custom/1?_format=json) json formatted response of a custom content type that I noticed, is enclosed in [], I get this result:

[
    {
        "user_id": "test",
        "title": "title"
    },
    ...
]

But I am not able to access either of the fields, tried user_id or title.

Other examples that is valid and invalid:

VALID by accessing data.type, displays article

{
    "data": {
        "type": "article",
        "id": "123",
    ...
    }
}

INVALID expression, tried id[0].value or id.value

[
    {
        "id": [
            {
                "value": 398
            }
        ],
    ...
    }
]

Parent issue

Tags : 8 json


Related Questions


How to use Icon8/webicons library with Drupal 7?

Updated May 04, 2016 08:03 AM

How to Logout a user using json?

Updated March 04, 2017 08:07 AM

Implementing schema.org

Updated May 18, 2017 17:07 PM

Disallow Json file in Drupal 7

Updated December 13, 2017 07:07 AM

How to return JSON data using Drupal 7

Updated August 22, 2015 17:03 PM