w3resource

Working with JSONPath and PHP

Description

In this tutorial, we will discuss using JSONPath with PHP to find and extract parts of the JSON data

If you are unfamiliar with JSONPath, please also read w3resource's JSONPath with JavaScript tutorial.

Obtain JSONPath

To work with JSONPath and PHP, you need to download jsonpath.php. You can download it from http://code.google.com/p/jsonpath/.

Once downloaded, you need to include the said PHP file in your webpage and you are ready to use it.

Syntax:

jsonPath(obj, expr [, args])

Parameters:

Parameter Description
obj (object|array) This parameter represents the Object representing the JSON structure.
expr (string) This parameter represents JSONPath expression string.
args (object|undefined) This parameter represents Object controlling path evaluation and output. As of this writing, only one member is supported.
args['resultType'] ("VALUE"|"PATH") By default, this parameter causes the result to be matching values. Else normalized path expressions.

Return Values

Return value Description
array An array comprising either values or normalized path expressions which match the path expression supplied as input.
false This is returned if no match is found.

Example of using JSONPath with PHP

The JSON we will be working with is as follows :

{ "MovieDatabase": {
  "movie": [ 
         	  { "name":"The Change-Up",
  "genre": "comedy",
  "director": "David Dobkin",
  "Facebook_like": 252
         	  },
         	  { "name":"Rise of the Planet of the Apes",
  "genre": "SciFi",
  "director": "Rupert Wyatt",
  "Facebook_like": 472
         	  },
         	  { "name":"30 Minutes or Less",
  "genre": "adventure",
  "director": "Ruben Fleischer",
  "Facebook_like": 114
         	  },
         	  { "name":"Final Destination 5",
  "genre": "Horror",
  "director": "Steven Quale",
  "Facebook_like": 241
         	  }
         	  ]
         	  }
         	  }

PHP Code to find and extract various parts of the above JSON data is as follows (note that since we will be using a JSON parser of Michal Migurski, you need to download and include that too ) :

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <title>PHP JSONPath example | JSON tutorial | w3resource</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  &</head>
  <body>
  <h1>This is an example of PHP with JSONPath</h1>
  <?php
         	  require_once('json.php');
         	  require_once('jsonpath-0.8.1.php');
         	  $json = '{ "MovieDatabase": {
  "movie": [
         	  { "name":"The Change-Up",
  "genre": "comedy",
  "director": "David Dobkin",
  "Facebook_like": 252
         	  },
         	  { "name":"Rise of the Planet of the Apes",
  "genre": "SciFi",
  "director": "Rupert Wyatt",
  "Facebook_like": 472
         	  },
         	  { "name":"30 Minutes or Less",
  "genre": "adventure",
  "director": "Ruben Fleischer",
  "Facebook_like": 114
         	  },
         	  { "name":"Final Destination 5",
  "genre": "Horror",
  "director": "Steven Quale",
  "Facebook_like": 241
         	  }
         	  ]
         	  }
         	  }';
         	  $parser = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
         	  $json_object = $parser->decode($json);
         	  $result = "";
         	  $result .= $parser->encode(jsonPath($json_object, "$.MovieDatabase.movie[*].director")) . "<strong> : find all 
         	  
         	  directors</strong>
  ";
         	  $result .= $parser->encode(jsonPath($json_object, "$..director")) . "<strong> : find all directors</strong>
  ";
         	  $result .= $parser->encode(jsonPath($json_object, "$.MovieDatabase.*")) . "<strong> : find all movies</strong>
  ";
         	  $result .= $parser->encode(jsonPath($json_object, "$.MovieDatabase..Facebook_like")) . "<strong> : find all facebook lies of 
         	  
         	  all the movies</strong>
  ";
         	  $result .= $parser->encode(jsonPath($json_object, "$..movie[(count(@)-1)]")) . "<strong> : the last movie in data</strong>
  ";
         	  $result .= $parser->encode(jsonPath($json_object, "$..movie[-1:]")) . "<strong> : the last movie in data</strong>
  ";
         	  $result .= $parser->encode(jsonPath($json_object, "$..movie[0,1]")) . "<strong> : the first two movies in data</strong>
  ";
         	  $result .= $parser->encode(jsonPath($json_object, "$..movie[:4]")) . "<strong> : the fourth movie in data</strong>
  ";
         	  $result .= $parser->encode(jsonPath($json_object, "$..movie[?(@['genre'])]")) . "<strong> : genres of all the movies in 
         	  
         	  data</strong>
  ";
         	  $result .= $parser->encode(jsonPath($json_object, "$..movie[?(@['Facebook_like']>200)]")) . "<strong> : movies with facebook 
         	  
         	  like more than 200</strong>
  ";
         	  $result .= $parser->encode(jsonPath($json_object, "$..*")) . "<strong> : All the info in data</strong>
  ";
         	  print($result);
         	  ?> 

View the example of working with JSONPath and PHP online.

Previous: Working with JSONPath and JavaScript
Next: Introduction to BSON



Follow us on Facebook and Twitter for latest update.