Home > front end >  Finding differences in json in Laravel
Finding differences in json in Laravel

Time:04-14

I am beginner in Laravel. I make my application in Laravel 8,

I have this value in DB:

$json = {
    "old": {
        "id": 1, 
        "hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681", 
        "car_id": 2, 
        "comments": "uwagi", 
        "driver_id": 1, 
        "address_to": "adres dostawy", 
        "carrier_id": 22, 
        "company_id": 1, 
        "created_at": "2022-04-14T09:07:41.000000Z", 
        "data_start": "2022-04-01", 
        "deleted_at": null, 
        "updated_at": "2022-04-14T09:07:41.000000Z", 
        "carrier_nip": "728-597-9477", 
        "data_finish": "2022-04-04", 
        "address_from": "adres wyjazdu", 
        "carrier_city": "Lake Emersonville", 
        "carrier_name": "Satterfield, Lebsack and O'Conner", 
        "order_number": "Numer zlecenia", 
        "speed_number": "Numer zlecenia speed - spedycja", 
        "carrier_email": "[email protected]", 
        "carrier_phone": "347.731.2768 x1508", 
        "dispatcher_id": 3, 
        "carrier_street": "6342 Pollich Crossroad", 
        "selected_files": "[7,8,9]", 
        "transport_type": 2, 
        "deliver_comments": null, 
        "transport_status": 3, 
        "is_neutral_option": 2,
        "selected_products": "[1,3]", 
        "transport_content": "kontakt", 
        "delivery_signature": null, 
        "selected_documents": "[5]", 
        "carrier_postal_code": "36804-7962", 
        "hidden_carrier_data": 0, 
        "delivery_name_surname": null, 
        "delivery_signature_binary": null, 
        "disable_change_status_driver": 0
    }, 
    "attributes": {
        "id": 1, 
        "hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681", 
        "car_id": 2, 
        "comments": "uwagi", 
        "driver_id": 1, 
        "address_to": 
        "adres dostawy", 
        "carrier_id": 22, 
        "company_id": 1, 
        "created_at": "2022-04-14T09:07:41.000000Z", 
        "data_start": "2022-04-01", 
        "deleted_at": null, 
        "updated_at": "2022-04-14T09:09:08.000000Z", 
        "carrier_nip": "728-597-9477", 
        "data_finish": "2022-04-04", 
        "address_from": "adres wyjazdu", 
        "carrier_city": "Lake Emersonville", 
        "carrier_name": "Satterfield, Lebsack and O'Conner", 
        "order_number": "Numer zlecenia", 
        "speed_number": "Numer zlecenia speed - spedycja", 
        "carrier_email": "[email protected]", 
        "carrier_phone": "347.731.2768 x1508", 
        "dispatcher_id": 4, 
        "carrier_street": "6342 Pollich Crossroad", 
        "selected_files": "[7,8,9]", 
        "transport_type": 2, 
        "deliver_comments": null, 
        "transport_status": 3, 
        "is_neutral_option": 2, 
        "selected_products": "[1,3,2]", 
        "transport_content": "kontakt", 
        "delivery_signature": null, 
        "selected_documents": "[5,6,4]", 
        "carrier_postal_code": "36804-7962", 
        "hidden_carrier_data": 0, 
        "delivery_name_surname": null, 
        "delivery_signature_binary": null, 
        "disable_change_status_driver": 0
        }
    }

I need show differences from OLD and Attributes. How can I make it?

Please help me.

CodePudding user response:

What about converting it to array by json_decode and then use array_diff? Like this:

$json = json_decode('{"old": {"id": 1, "hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681", "car_id": 2, "comments": "uwagi", "driver_id": 1, "address_to": "adres dostawy", "carrier_id": 22, "company_id": 1, "created_at": "2022-04-14T09:07:41.000000Z", "data_start": "2022-04-01", "deleted_at": null, "updated_at": "2022-04-14T09:07:41.000000Z", "carrier_nip": "728-597-9477", "data_finish": "2022-04-04", "address_from": "adres wyjazdu", "carrier_city": "Lake Emersonville", "carrier_name": "Satterfield, Lebsack and OConner", "order_number": "Numer zlecenia", "speed_number": "Numer zlecenia speed - spedycja", "carrier_email": "[email protected]", "carrier_phone": "347.731.2768 x1508", "dispatcher_id": 3, "carrier_street": "6342 Pollich Crossroad", "selected_files": "[7,8,9]", "transport_type": 2, "deliver_comments": null, "transport_status": 3, "is_neutral_option": 2, "selected_products": "[1,3]", "transport_content": "kontakt", "delivery_signature": null, "selected_documents": "[5]", "carrier_postal_code": "36804-7962", "hidden_carrier_data": 0, "delivery_name_surname": null, "delivery_signature_binary": null, "disable_change_status_driver": 0}, "attributes": {"id": 1, "hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681", "car_id": 2, "comments": "uwagi", "driver_id": 1, "address_to": "adres dostawy", "carrier_id": 22, "company_id": 1, "created_at": "2022-04-14T09:07:41.000000Z", "data_start": "2022-04-01", "deleted_at": null, "updated_at": "2022-04-14T09:09:08.000000Z", "carrier_nip": "728-597-9477", "data_finish": "2022-04-04", "address_from": "adres wyjazdu", "carrier_city": "Lake Emersonville", "carrier_name": "Satterfield, Lebsack and OConner", "order_number": "Numer zlecenia", "speed_number": "Numer zlecenia speed - spedycja", "carrier_email": "[email protected]", "carrier_phone": "347.731.2768 x1508", "dispatcher_id": 4, "carrier_street": "6342 Pollich Crossroad", "selected_files": "[7,8,9]", "transport_type": 2, "deliver_comments": null, "transport_status": 3, "is_neutral_option": 2, "selected_products": "[1,3,2]", "transport_content": "kontakt", "delivery_signature": null, "selected_documents": "[5,6,4]", "carrier_postal_code": "36804-7962", "hidden_carrier_data": 0, "delivery_name_surname": null, "delivery_signature_binary": null, "disable_change_status_driver": 0}}', true);

var_dump(array_diff($json['old'], $json['attributes']));

In result I get:

array(2) {
  ["selected_products"]=>
  string(5) "[1,3]"
  ["selected_documents"]=>
  string(3) "[5]"
}

CodePudding user response:

You can use array_diff https://www.php.net/manual/en/function.array-diff.php

<?php

$json = '{
    "old": {
        "id": 1, 
        "hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681", 
        "car_id": 2, 
        "comments": "uwagi", 
        "driver_id": 1, 
        "address_to": "adres dostawy", 
        "carrier_id": 22, 
        "company_id": 1, 
        "created_at": "2022-04-14T09:07:41.000000Z", 
        "data_start": "2022-04-01", 
        "deleted_at": null, 
        "updated_at": "2022-04-14T09:07:41.000000Z", 
        "carrier_nip": "728-597-9477", 
        "data_finish": "2022-04-04", 
        "address_from": "adres wyjazdu", 
        "carrier_city": "Lake Emersonville", 
        "carrier_name": "Satterfield, Lebsack and O\'Conner", 
        "order_number": "Numer zlecenia", 
        "speed_number": "Numer zlecenia speed - spedycja", 
        "carrier_email": "[email protected]", 
        "carrier_phone": "347.731.2768 x1508", 
        "dispatcher_id": 3, 
        "carrier_street": "6342 Pollich Crossroad", 
        "selected_files": "[7,8,9]", 
        "transport_type": 2, 
        "deliver_comments": null, 
        "transport_status": 3, 
        "is_neutral_option": 2,
        "selected_products": "[1,3]", 
        "transport_content": "kontakt", 
        "delivery_signature": null, 
        "selected_documents": "[5]", 
        "carrier_postal_code": "36804-7962", 
        "hidden_carrier_data": 0, 
        "delivery_name_surname": null, 
        "delivery_signature_binary": null, 
        "disable_change_status_driver": 0
    }, 
    "attributes": {
        "id": 1, 
        "hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681", 
        "car_id": 2, 
        "comments": "uwagi", 
        "driver_id": 1, 
        "address_to": 
        "adres dostawy", 
        "carrier_id": 22, 
        "company_id": 1, 
        "created_at": "2022-04-14T09:07:41.000000Z", 
        "data_start": "2022-04-01", 
        "deleted_at": null, 
        "updated_at": "2022-04-14T09:09:08.000000Z", 
        "carrier_nip": "728-597-9477", 
        "data_finish": "2022-04-04", 
        "address_from": "adres wyjazdu", 
        "carrier_city": "Lake Emersonville", 
        "carrier_name": "Satterfield, Lebsack and O\'Conner", 
        "order_number": "Numer zlecenia", 
        "speed_number": "Numer zlecenia speed - spedycja", 
        "carrier_email": "[email protected]", 
        "carrier_phone": "347.731.2768 x1508", 
        "dispatcher_id": 4, 
        "carrier_street": "6342 Pollich Crossroad", 
        "selected_files": "[7,8,9]", 
        "transport_type": 2, 
        "deliver_comments": null, 
        "transport_status": 3, 
        "is_neutral_option": 2, 
        "selected_products": "[1,3,2]", 
        "transport_content": "kontakt", 
        "delivery_signature": null, 
        "selected_documents": "[5,6,4]", 
        "carrier_postal_code": "36804-7962", 
        "hidden_carrier_data": 0, 
        "delivery_name_surname": null, 
        "delivery_signature_binary": null, 
        "disable_change_status_driver": 0
        }
    }';
    
$array = \json_decode($json, true);
$diff = \array_diff($array['old'], $array['attributes']);

\var_dump($diff);

Which would output:

array(2) {
  ["selected_products"]=>
  string(5) "[1,3]"
  ["selected_documents"]=>
  string(3) "[5]"
}

See the example here https://3v4l.org/A3ck7

CodePudding user response:

I wrote a code for you that only returns the differences in an array. array_diff() seems to be a good alternative, but running my script, it script found more differences than the array_diff() method.

NOTE that I'm strictly comparing in the foreach loop, but I assumed that the data are from the same table, just different records, so the types shouldn't matter. Regardless, if you want to have all differences, then IMO strictly comparing is the way to go.

$differences = [];
$json = '{"old": {"id": 1, "hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681", "car_id": 2, "comments": "uwagi", "driver_id": 1, "address_to": "adres dostawy", "carrier_id": 22, "company_id": 1, "created_at": "2022-04-14T09:07:41.000000Z", "data_start": "2022-04-01", "deleted_at": null, "updated_at": "2022-04-14T09:07:41.000000Z", "carrier_nip": "728-597-9477", "data_finish": "2022-04-04", "address_from": "adres wyjazdu", "carrier_city": "Lake Emersonville", "carrier_name": "Satterfield, Lebsack and O\'Conner", "order_number": "Numer zlecenia", "speed_number": "Numer zlecenia speed - spedycja", "carrier_email": "[email protected]", "carrier_phone": "347.731.2768 x1508", "dispatcher_id": 3, "carrier_street": "6342 Pollich Crossroad", "selected_files": "[7,8,9]", "transport_type": 2, "deliver_comments": null, "transport_status": 3, "is_neutral_option": 2, "selected_products": "[1,3]", "transport_content": "kontakt", "delivery_signature": null, "selected_documents": "[5]", "carrier_postal_code": "36804-7962", "hidden_carrier_data": 0, "delivery_name_surname": null, "delivery_signature_binary": null, "disable_change_status_driver": 0}, "attributes": {"id": 1, "hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681", "car_id": 2, "comments": "uwagi", "driver_id": 1, "address_to": "adres dostawy", "carrier_id": 22, "company_id": 1, "created_at": "2022-04-14T09:07:41.000000Z", "data_start": "2022-04-01", "deleted_at": null, "updated_at": "2022-04-14T09:09:08.000000Z", "carrier_nip": "728-597-9477", "data_finish": "2022-04-04", "address_from": "adres wyjazdu", "carrier_city": "Lake Emersonville", "carrier_name": "Satterfield, Lebsack and O\'Conner", "order_number": "Numer zlecenia", "speed_number": "Numer zlecenia speed - spedycja", "carrier_email": "[email protected]", "carrier_phone": "347.731.2768 x1508", "dispatcher_id": 4, "carrier_street": "6342 Pollich Crossroad", "selected_files": "[7,8,9]", "transport_type": 2, "deliver_comments": null, "transport_status": 3, "is_neutral_option": 2, "selected_products": "[1,3,2]", "transport_content": "kontakt", "delivery_signature": null, "selected_documents": "[5,6,4]", "carrier_postal_code": "36804-7962", "hidden_carrier_data": 0, "delivery_name_surname": null, "delivery_signature_binary": null, "disable_change_status_driver": 0}}';
$jsonArray = json_decode($json, true);

// Assuming that $jsonArray["old"] and $jsonArray["attributes"] have the same elements
foreach ($jsonArray["old"] as $key => $value) {
    if ($value !== $jsonArray["attributes"][$key])
        $differences[$key] = [
            "old" => $value,
            "attributes" => $jsonArray["attributes"][$key]
        ];
}

Result I got:

Array
(
    [updated_at] => Array
        (
            [old] => 2022-04-14T09:07:41.000000Z
            [attributes] => 2022-04-14T09:09:08.000000Z
        )

    [dispatcher_id] => Array
        (
            [old] => 3
            [attributes] => 4
        )

    [selected_products] => Array
        (
            [old] => [1,3]
            [attributes] => [1,3,2]
        )

    [selected_documents] => Array
        (
            [old] => [5]
            [attributes] => [5,6,4]
        )
)

CodePudding user response:

First of all, you have to seprate both of the objects into two sub objects 1 is old and 2 is attribute

Like:

$old = '{
        "id": 1, 
        "hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681", 
        "car_id": 2, 
        "comments": "uwagi", 
        "driver_id": 1, 
        "address_to": "adres dostawy", 
        "carrier_id": 22, 
        "company_id": 1, 
        "created_at": "2022-04-14T09:07:41.000000Z", 
        "data_start": "2022-04-01", 
        "deleted_at": null, 
        "updated_at": "2022-04-14T09:07:41.000000Z", 
        "carrier_nip": "728-597-9477", 
        "data_finish": "2022-04-04", 
        "address_from": "adres wyjazdu", 
        "carrier_city": "Lake Emersonville", 
        "carrier_name": "Satterfield, Lebsack and O\'Conner", 
        "order_number": "Numer zlecenia", 
        "speed_number": "Numer zlecenia speed - spedycja", 
        "carrier_email": "[email protected]", 
        "carrier_phone": "347.731.2768 x1508", 
        "dispatcher_id": 3, 
        "carrier_street": "6342 Pollich Crossroad", 
        "selected_files": "[7,8,9]", 
        "transport_type": 2, 
        "deliver_comments": null, 
        "transport_status": 3, 
        "is_neutral_option": 2,
        "selected_products": "[1,3]", 
        "transport_content": "kontakt", 
        "delivery_signature": null, 
        "selected_documents": "[5]", 
        "carrier_postal_code": "36804-7962", 
        "hidden_carrier_data": 0, 
        "delivery_name_surname": null, 
        "delivery_signature_binary": null, 
        "disable_change_status_driver": 0
    }'; 

 $attribute = '{
        "id": 1, 
        "hash": "19f149b6-f7e2-4d61-b3c5-d46ebc92f681", 
        "car_id": 2, 
        "comments": "uwagi", 
        "driver_id": 1, 
        "address_to": 
        "adres dostawy", 
        "carrier_id": 22, 
        "company_id": 1, 
        "created_at": "2022-04-14T09:07:41.000000Z", 
        "data_start": "2022-04-01", 
        "deleted_at": null, 
        "updated_at": "2022-04-14T09:09:08.000000Z", 
        "carrier_nip": "728-597-9477", 
        "data_finish": "2022-04-04", 
        "address_from": "adres wyjazdu", 
        "carrier_city": "Lake Emersonville", 
        "carrier_name": "Satterfield, Lebsack and O\'Conner", 
        "order_number": "Numer zlecenia", 
        "speed_number": "Numer zlecenia speed - spedycja", 
        "carrier_email": "[email protected]", 
        "carrier_phone": "347.731.2768 x1508", 
        "dispatcher_id": 4, 
        "carrier_street": "6342 Pollich Crossroad", 
        "selected_files": "[7,8,9]", 
        "transport_type": 2, 
        "deliver_comments": null, 
        "transport_status": 3, 
        "is_neutral_option": 2, 
        "selected_products": "[1,3,2]", 
        "transport_content": "kontakt", 
        "delivery_signature": null, 
        "selected_documents": "[5,6,4]", 
        "carrier_postal_code": "36804-7962", 
        "hidden_carrier_data": 0, 
        "delivery_name_surname": null, 
        "delivery_signature_binary": null, 
        "disable_change_status_driver": 0
        }';

After that:

$old_array = json_decode($old, TRUE);
$attribute_array = json_decode($attribute, TRUE);
$result_array = array_diff($old_array,$attribute_array);
    
    
if(!empty($result_array[0])){     
    echo "They are different";
}

It will work like when you have old array and attribute array

Old array has ABC key and attribute array doesn't have then it will show message

We are using json_decode, it will use to convert JSON into assosiative array and you must have to pass true as second attribute for making it associative

Maybe it will helpful

  • Related