1

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

  • Yep so your custom views endpoint will be diffirent structure from default node due to the way views encodes the data ... so could it be as simple as makeing new test for the endpoint and follow its new structure i allways just use json_decode / array_key_exist for this kindof thing – Taggart Jensen May 30 '18 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.