Current Location: Home> Latest Articles> How to Efficiently Handle Nested JSON Objects in PHP and MySQL (With Code Examples)

How to Efficiently Handle Nested JSON Objects in PHP and MySQL (With Code Examples)

M66 2025-06-22

How to Handle Nested JSON Objects in PHP and MySQL

In modern web development, JSON (JavaScript Object Notation) is widely used for data exchange between front-end and back-end systems. Nested JSON structures are common and can be tricky to work with. This article walks through how to handle these structures using PHP and how to store them in a MySQL database.

Parsing Nested JSON in PHP

PHP provides two native functions for working with JSON: json_decode() for converting JSON strings into PHP variables, and json_encode() for converting PHP variables into JSON format.

Here’s an example of a nested JSON object:

{
    "name": "John",
    "age": 25,
    "address": {
        "street": "123 Main St",
        "city": "New York",
        "state": "NY"
    }
}

To convert this JSON string into a PHP object:

$json = '{
    "name": "John",
    "age": 25,
    "address": {
        "street": "123 Main St",
        "city": "New York",
        "state": "NY"
    }
}';

$object = json_decode($json);

You can now access nested values like this:

$name = $object->name;              // John
$age = $object->age;                // 25
$street = $object->address->street; // 123 Main St
$city = $object->address->city;     // New York
$state = $object->address->state;   // NY

Converting PHP Objects to JSON

To convert a nested PHP object back to a JSON string:

$object = new stdClass;
$object->name = 'John';
$object->age = 25;
$object->address = new stdClass;
$object->address->street = '123 Main St';
$object->address->city = 'New York';
$object->address->state = 'NY';

$json = json_encode($object);

This will result in the same JSON structure as the original example.

Storing Nested JSON Data in MySQL

Assume you have a MySQL table called users with the following fields: id, name, age, street, city, and state. To store nested JSON data, you'll first convert the JSON to a PHP array:

$array = json_decode($json, true);

Then use PDO to connect and insert the data into the database:

$dsn = 'mysql:host=localhost;dbname=mydatabase';
$username = 'username';
$password = 'password';

$pdo = new PDO($dsn, $username, $password);

$stmt = $pdo->prepare('INSERT INTO users (name, age, street, city, state) VALUES (:name, :age, :street, :city, :state)');
$stmt->bindParam(':name', $array['name']);
$stmt->bindParam(':age', $array['age']);
$stmt->bindParam(':street', $array['address']['street']);
$stmt->bindParam(':city', $array['address']['city']);
$stmt->bindParam(':state', $array['address']['state']);
$stmt->execute();

This script safely inserts each nested field into its corresponding database column.

Conclusion

By using PHP’s JSON handling functions in combination with PDO for MySQL, you can efficiently work with nested JSON data. Whether you’re building APIs or managing structured data, mastering these techniques will help you handle complex data formats more effectively.