You are here

protected function FeedsJSONPathParser::jsonPath in Feeds JSONPath Parser 7

Same name and namespace in other branches
  1. 6 FeedsJSONPathParser.inc \FeedsJSONPathParser::jsonPath()

Searches an array via JSONPath.

Parameters

array $array: The input array to parse.

string $expression: The JSONPath expression.

Return value

array Returns the parsed jsonpath expression.

Throws

RuntimeException In case the parsed json is not an array.

2 calls to FeedsJSONPathParser::jsonPath()
FeedsJSONPathParser::parse in ./FeedsJSONPathParser.inc
Implements FeedsParser::parse().
FeedsJSONPathParser::parseSourceElement in ./FeedsJSONPathParser.inc
Parses one item from the context array.

File

./FeedsJSONPathParser.inc, line 141
Contains FeedsJSONPathParser.

Class

FeedsJSONPathParser
Parses JSON using JSONPath.

Code

protected function jsonPath($array, $expression) {
  $result = (new JSONPath($array))
    ->find($expression)
    ->data();

  // If the returned result is empty, just return an empty array.
  if (empty($result)) {
    return array();
  }

  // If the parsed json is not an array, throw an exception.
  if (!is_array($result)) {
    throw new RuntimeException(t('The parsed json must return an array.'));
  }
  return $result;
}