DEV Community

Temuri Takalandze
Temuri Takalandze

Posted on

3

Create PHP Object from JSON Content

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"
//    }
//  }
//}

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (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

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay