Skip to main content

Overview

The Objectify class provides utility methods for converting JSON strings to PHP arrays. It includes support for recursive mapping and handling arrays of JSON strings.

Class Reference

Methods

json_mapper()

Decode JSON strings to arrays with optional recursive processing.
mixed
required
The value to process. Can be:
  • JSON string: Will be decoded to array
  • Array: Will recursively process each element (if $recursive is true)
  • Other: Returns unchanged
bool
default:"true"
Whether to recursively process array elements
mixed
  • Decoded array if input is valid JSON string
  • Recursively processed array if input is array and $recursive is true
  • Original value if not JSON or array

Example Usage


json_mapper_norecurse()

Decode JSON strings without recursive processing (alias for json_mapper() with recursive = false).
mixed
required
The value to process
mixed
Decoded value without recursive array processing

Example Usage


json_to_array()

Convert JSON strings in an array to decoded arrays with optional recursive processing.
mixed
required
Array to process. If not an array, it will be converted to a single-element array
bool
default:"true"
Whether to recursively process nested elements
array
Array with JSON strings decoded to arrays

Example Usage


Use Cases

Processing API Responses

Batch Processing JSON Data

Handling Nested JSON


Complete Example


Notes

The json_mapper() method will return the original value unchanged if:
  • The value is not a string
  • The string is not valid JSON
  • JSON decoding fails
When using json_to_array(), if the input is not an array, it will be wrapped in an array first. This means a single JSON string will become an array containing the decoded data.
Use json_mapper_norecurse() when you have a mixed array where only top-level JSON strings should be decoded, and nested structures should remain unchanged.