Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONPath Query to get Node-names?

Tags:

json

php

jsonpath

Consider the following piece of JSONPath:

{
   "result":[
      {
         "type":"Residence",
         "street":"Piazza di Spagna",
         "city":"-4:0"
      },
      {
         "type":"Residence",
         "street":"test",
         "city":"-4:1"
      }
   ]
}

Is it possible to get a list of all the node-names?
So for example, I want a list like: type, street, city.

like image 803
Larry Avatar asked May 06 '12 17:05

Larry


People also ask

What does JsonPath return?

JSONPath is a query language for JSON, similar to XPath for XML. It allows you to select and extract data from a JSON document. You use a JSONPath expression to traverse the path to an element in the JSON structure.

What is JsonPath syntax?

JSONPath Syntax. Last modified on November 22, 2021. JSONPath is a query language for JSON, similar to XPath for XML. AlertSite API endpoint monitors let you use JSONPath in assertions to specify the JSON fields that need to be verified.

How do I specify JsonPath?

A JsonPath expression begins with the dollar sign ( $ ) character, which refers to the root element of a query. The dollar sign is followed by a sequence of child elements, which are separated via dot (code) notation or via the square brackets (code).

What is Jayway JsonPath?

Jayway JsonPath is a Java port of Stefan Goessner JsonPath implementation.


1 Answers

Try this

$arr = (json_decode($json)->result[0]);
 $array = get_object_vars($arr);
 $properties = array_keys($array);
 print_r($properties);`

Out put will be

Array
(
    [0] => type
    [1] => street
    [2] => city
)
like image 154
Web Artisan Avatar answered Sep 24 '22 09:09

Web Artisan