PHP - compare the structure of two JSON objects

I have two JSON objects , and I would like to compare their structure . How can i do this?

These objects are generated on the fly and depending on the dynamic content . This means that objects are always different , but most of the time they have the same structure . I want to be able to track changes after they appear .

An example . These two objects should be considered equal , since both have the structure of the same structure : the var index and tags .. p>

{ "index": 0, "tags": [ "abc" ] } { "index": 1, "tags": [ "xyz" ] } 

Thoughts?

+5
source share
4 answers

## You can use this TreeWalker php library. ##

TreeWalker - simple and small API in php
(I developed this library, hope it helps you)

He offers two methods
1- Get json difference
2- Change json value (recursively)

this method will return the difference between json1 and json2

 $struct1 = array("casa"=>1, "b"=>"5", "cafeina"=>array("ss"=>"ddd"), "oi"=>5); $struct2 = array("casa"=>2, "cafeina"=>array("ss"=>"dddd"), "oi2"=>5); //Ps print_r($treeWalker->getdiff($struct1, $struct2)) { new: { b: "5", oi: 5 }, removed: { oi2: 5 }, edited: { casa: { oldvalue: 2, newvalue: 1 }, cafeina/ss: { oldvalue: "dddd", newvalue: "ddd" } }, time: 0 } 
+7
source

This is a little rude, but you get an image;

 $json = '[ { "index": 0, "tags": [ "abc" ] }, { "index": 1, "tags": [ "xyz" ] }, { "foo": 2, "bar": [ "xyz" ] }]'; $array = json_decode($json, true); $default = array_keys($array[0]); $error = false; $errors = array(); foreach ($array as $index => $result): foreach ($default as $search): if (!isset($result[$search])): $error = true; $errors[] = "Property '{$search}' at entry '{$index}' not found. "; endif; endforeach; endforeach; if ($error): echo 'Objects are not the same. '; foreach ($errors as $message): echo $message; endforeach; endif; 

returns:

Objects do not match. Property 'index' at input '2' not found. The 'tags' property was not found while writing '2'.

+1
source

You mean by structure, for example, an array of a model, for example:

 array ( 'index' => int, 'tags' => array() ) 

If this is what you are trying to get, try this ...

 $arr1 = array ( array ( 'index' => 0, 'tags' => ['abc'] ), array ( 'index' => 1, 'tags' => ['xyz'] ), array ( 'index' => 2, 'tags' => ['xyz'], 'boom' => 'granade' ), array ( 'index' => 3, 'tags' => 'xyz' ) ); $holder = array(); $model = array ('index' => 0, 'tags' => array()); for ($i = 0;$i < count($arr1); $i++) { $holder = array_diff(array_merge_recursive($arr1[$i], $model), $model); if (!empty($holder)) { echo "different structure<br>"; } else { echo "same structure<br>"; // for further validation /* $keys = array_keys($model); if (is_int($arr1[$i][$keys[0]]) && is_array($arr1[$i][$keys[1]])) echo "same structure<br>"; else echo "different structure<br>"; */ } } 

Output Example:

 same structure same structure different structure different structure 
0
source

You can convert the json string to a php array and then use the array_diff function ($ arr1, $ arr2) to compare the newly created array with another array, the result is an array containing the elements of the first array that does not exist in another array

example:

 <?php $array1 = '{"name":"myname","age":"40"}'; //convert the obtained stdclass object to an array $array1 = (array) json_decode($array1); $array2 = array("name"=>"myname123","age"=>10); print_r($array2); $result_array = array_diff($array1,$array2); if(empty($result_array[0])){ echo "they have the same structure "; } ?> 
-1
source

All Articles