11

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.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
Larry
  • 11,439
  • 15
  • 61
  • 84

7 Answers7

5

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
)
maxhb
  • 8,554
  • 9
  • 29
  • 53
Web Artisan
  • 1,870
  • 3
  • 23
  • 33
4

On PHP >= 5.4 you can obtain your keys with one line of code:

$nodeNames = array_keys( json_decode( $jsonString, True )['result'][0] );

3v4l.org demo

On lower versions (PHP >= 5.2.16), you have to break above code in two lines:

$array = json_decode( $jsonString, True );
$nodeNames = array_keys( $array['result'][0] );

I decode the JSON string with second parameter as True to force result as array, then I call array_keys to obtain the keys of array['result'][0].

Edit: more flexibility

Your example can be processed as above without problems, but what happens if original JSON string has different keys? The above solution will fail if in the first result row there are not all the keys. To obtain all the keys, you can use this code:

$array  = json_decode( $jsonString, True );
$nodeNames = array();
foreach( $array['result'] as $row )
{
    $nodeNames = array_unique( array_merge( $nodeNames, array_keys( $row ) ) );
}

or, using array_filter, with this code:

$array  = json_decode( $jsonString, True );
$nodeNames = array();
array_filter
(
    $array['result'], 
    function( $row )
    {
        global $nodeNames;
        $nodeNames = array_unique( array_merge( $nodeNames, array_keys( $row ) ) );
    }
);

By this two equivalent examples, I process each result row, merging the keys in $nodeNames array and using array_unique to delete duplicate keys.


fusion3k
  • 11,568
  • 4
  • 25
  • 47
1

You can use following function to print all keys in an array format

print_r(array_keys($array));

1

Is the JSON Path definitely going to follow that structure every time? As in Result => Arrays all of which have the same Nodes.

If it does then the following will work:

function getJsonNodes($json) {
    $nodes   = array();
    $decoded = json_decode($json, true);

    if (json_last_error() !== JSON_ERROR_NONE) {
        throw new \InvalidArgumentException('Invalid JSON String passed to getJsonNodes()');
    }

    $result = $decoded['result'];
    if (is_array($result)) {
        $nodes = array_keys($result[0]);
    }

    return $nodes;
}

Usage would be something like:

try {
    $nodes = getJsonNodes($json);
} catch (\InvalidArgumentException $e) {
    echo $e->getMessage();
}

Means you can catch any Invalid JSON strings that could potentially be passed and mess with the output.

Although as I stated, the above solution will only work if your JSON Path follows the structure to put in your OP.

You can see it in use here: https://ideone.com/dlvdu2

Hope it helps either way.

Mikey
  • 2,606
  • 1
  • 12
  • 20
0

Is it possible to get a list of all the node-names?

$object = json_decode($json);
$json_array = $object->result;
foreach ($json_array as $key => $value) {
    $object_var = get_object_vars($value);
    $object_key = array_keys($object_var);
    var_dump($object_key);//will get all node_names!
}
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
-1

Try this

$result[0].type , $result[0].street, $result[0].city
j0k
  • 22,600
  • 28
  • 79
  • 90
user1677986
  • 61
  • 1
  • 1
  • 7
-2

Please check the below code ..Hope will work

<?php
$text[]=array(
            result=>array(
                "type"=>"Residence",
                "street"=>"pizza",
                "city"=>"ini"
            ),
            array(
                "type"=>"Residence",
                "street"=>"pizza",
                "city"=>"ini"
            )
        );

echo json_encode($text);
?>
B8vrede
  • 4,432
  • 3
  • 27
  • 40