PHP - compare the structure of two JSON objects

10,531

Solution 1

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

TreeWalker is a simple and smal API in php
(I developed this library, i hope it helps you)

It offers two methods
1- Get json difference
2- Edit json value (Recursively)

this method will return the diference 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);

//P.s
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
}

Solution 2

It's a bit rough, but you get the picture;

$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 are not the same. Property 'index' at entry '2' not found. Property 'tags' at entry '2' not found.

Share:
10,531
Boarking
Author by

Boarking

Updated on June 19, 2022

Comments

  • Boarking
    Boarking almost 2 years

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

    Those object are being generated on-the-fly and depending on dynamic content. Which means that the objects are always different but most of the time have the same structure. I want to be able to catch the changes once they occur.

    Example: These two objects should be considered as equal, because both have the same structure: index var and tags array.

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

    Thoughts?

  • Boarking
    Boarking over 8 years
    The problem here is with the default array. How can I generate it? All I have is two JSON objects. array_keys() is also not a good option because it is not recursive. My actual objects are very big and have many levels of nesting. I have to be able to compare everything.
  • vonUbisch
    vonUbisch over 8 years
    Is it not possible to expect a certain structure? As far as I know, to do this efficiently you will need to expect something.
  • Boarking
    Boarking over 8 years
    I have no idea of what I am expecting. All I know is that I have two JSON objects which likely the same but I have to be sure before I proceed.
  • CSSBurner
    CSSBurner almost 6 years
    I used this library on my current work project - worked great for my purposes (needed a diff). Two thumbs up
  • Martin Sloan
    Martin Sloan over 4 years
    I just used this package as well and it works great. Saved me a lot of time, thank you!