You are here

function shorten_get_value_from_json in Shorten URLs 8.2

Extracts a value from a json object given a json member name or path.

Parameters

array $contents: The decoded json object containing the data to extract.

string $expression: A single json member name or a dot notation path expression.

Return value

string A value from the json object.

1 call to shorten_get_value_from_json()
shorten_fetch in ./shorten.module
Downloads the response of the URL abbreviation service.

File

./shorten.module, line 399

Code

function shorten_get_value_from_json($contents, $expression) {
  $exploded_expression = str_getcsv($expression, '.', '"', '\\');
  if (count($exploded_expression) === 1) {
    if (!isset($contents[$expression])) {
      \Drupal::logger('shorten')
        ->error('json response does not contain specified member name or path expression: @expression', [
        '@expression' => $expression,
      ]);
      return NULL;
    }
    return $contents[$expression];
  }
  return shorten_get_path_in_array($contents, $exploded_expression);
}