DEV Community

loading...

Create PHP Object from JSON Content

Temuri Takalandze
Middle Software Engineer, having over 4 years of experience in Back-End development; The author of several PHP libraries and Drupal modules; Open Source Contributor.
・1 min read

Consider that you have example.json with the following content:

{
  "firstName": "Temuri",
  "lastName": "Takalandze",
  "active": true,
  "position": {
    "title": "Developer",
    "department": {
      "title": "IT"
    }
  }
}

and several POPO classes to represent this JSON data:

Department.php

<?php

class Department
{
    /**
     * @var string
     */
    private $title;

    // Getters and Setters here...
}

Position.php

<?php

class Position
{
    /**
     * @var string
     */
    private $title;

    /**
     * @var \ABGEO\POPO\Example\Department
     */
    private $department;

    // Getters and Setters here...
}

Person.php

<?php

class Person
{
    /**
     * @var string
     */
    private $firstName;

    /**
     * @var string
     */
    private $lastName;

    /**
     * @var bool
     */
    private $active;

    /**
     * @var \ABGEO\POPO\Example\Position
     */
    private $position;

    // Getters and Setters here...
}

Now you want to convert this JSON to POPO with relations. The ABGEO07/json-to-popo package gives you this ability. Install it using Composer:
composer require abgeo/json-to-popo

Now let's create new ABGEO\POPO\Composer object and read example.json content:

$composer = new Composer();
$jsonContent = file_get_contents(__DIR__ . '/example.json');

Time for magic! Call composeObject() with the contents of JSON and the main class, and this will give you POPO:

$resultObject = $composer->composeObject($jsonContent, Person::class);

Print $resultObject:

var_dump($resultObject);

//class ABGEO\POPO\Example\Person#2 (4) {
//  private $firstName =>
//  string(6) "Temuri"
//  private $lastName =>
//  string(10) "Takalandze"
//  private $active =>
//  bool(true)
//  private $position =>
//  class ABGEO\POPO\Example\Position#4 (2) {
//    private $title =>
//    string(9) "Developer"
//    private $department =>
//    class ABGEO\POPO\Example\Department#7 (1) {
//      private $title =>
//      string(2) "IT"
//    }
//  }
//}

Discussion (2)

Collapse
mrbenosborne profile image
Ben Osborne

Hi, I started work on something similar but uses PHP 8 attributes, take a look github.com/mrbenosborne/json-unmar....

<?php

use JSON\Attributes\JSON;
use JSON\Unmarshal;

include '../vendor/autoload.php';
include 'FlightRoute.php';

/**
 * Class Flight
 */
class Flight
{
    #[JSON(field: 'airline')]
    public string $airlineName;

    #[JSON(field: 'aircraft.type')]
    public string $aircraftType;

    #[JSON(field: 'route', type: FlightRoute::class)]
    public array $route;
}

// Create a new flight class
$flight = new Flight();

// Load our JSON data from file
$jsonData = json_decode(file_get_contents('flight.json'), true);

// Unmarshal JSON
Unmarshal::decode($flight, $jsonData);
Enter fullscreen mode Exit fullscreen mode
Collapse
sirzarganwar profile image
Martin Jirasek

Hi, this is better for me. github.com/Tharos/Schematic