3

I have two different(separate) json format response and I am doing a search for element with values in each.

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);
    }
}

One example starts with a curly braces which I believe an object.

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

and I was able to access the nid value by typing nid[0].value

However, when I have a json response that is enclosed in []:

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

I dont get a valid expression when I try test

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
            }
        ],
    ...
    }
]
lauda
  • 4,153
  • 2
  • 14
  • 28

2 Answers2

0

If you need values from a Json string then you can use json-decode to create an array from Json, or you could extract the values from the Json string using regular expressions.

lauda
  • 4,153
  • 2
  • 14
  • 28
0

[ {...}, {...} ]

is a jsonArray and

{ ..., ... }

is a json object.

Try accessing json object from json array using getJSONObject(index) method on jsonArray and then you can access values inside json object.