Sai A Sai A
Updated date Nov 19, 2023
In this blog, we will explore two methods for converting a JSON string with pretty print formatting into a PHP array.

Introduction:

JSON (JavaScript Object Notation) is a widely used data interchange format for transmitting and storing structured data. It is both human-readable and machine-understandable, making it a popular choice for data exchange between web applications and APIs. However, when working with JSON data, you might encounter JSON strings with pretty print formatting – line breaks and indentation for human readability. In this blog, we will show you how to convert such formatted JSON strings into PHP arrays, making it easier to work with the data in your PHP applications.

Method 1: Using PHP's json_decode Function

The simplest way to convert a JSON string to a PHP array is by using the built-in json_decode function. This method works for both compact and pretty print JSON formats.

// JSON string with pretty print formatting
$jsonString = '
{
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}';

// Convert JSON string to PHP array
$arrayData = json_decode($jsonString, true);

// Output the resulting PHP array
print_r($arrayData);

Output:

Array
(
    [name] => John Doe
    [age] => 30
    [city] => New York
)
  • We start by defining a JSON string with pretty print formatting, encapsulated in single quotes.
  • To convert the JSON string into a PHP array, we use the json_decode function. The second argument, true, specifies that we want an associative array. If set to false, it would return an object.
  • Finally, we use print_r to display the resulting PHP array.

Method 2: Removing Whitespace and Line Breaks

Another method to convert pretty print JSON to a PHP array is by removing the whitespace and line breaks from the JSON string before parsing it with json_decode.

// JSON string with pretty print formatting
$jsonString = '
{
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}';

// Remove whitespace and line breaks
$compactJsonString = preg_replace('/\s+/', '', $jsonString);

// Convert compact JSON string to PHP array
$arrayData = json_decode($compactJsonString, true);

// Output the resulting PHP array
print_r($arrayData);

Output:

Array
(
    [name] => John Doe
    [age] => 30
    [city] => New York
)
  • First, we define a JSON string with pretty print formatting.
  • To convert this string into a compact, non-pretty-printed format, we use a regular expression to remove all whitespace and line breaks using preg_replace. The regular expression \s+ matches one or more whitespace characters (including spaces, tabs, and line breaks) and replaces them with an empty string.
  • The resulting compact JSON string is then parsed into a PHP array using json_decode.
  • Finally, we print the PHP array using print_r.

These two methods are simple and effective for converting pretty print JSON strings into PHP arrays. You can choose the one that best fits your needs and coding style. However, keep in mind that Method 1 is generally preferred for its simplicity and readability.

Conclusion:

In this blog, we have covered two methods for converting a JSON string with pretty print formatting into a PHP array. The first method uses PHP's built-in json_decode function, which works seamlessly with both compact and pretty print JSON. The second method involves removing whitespace and line breaks from the JSON string before parsing it with json_decode. Both methods produce the same output, allowing you to work with the JSON data in your PHP applications.

Comments (0)

There are no comments. Be the first to comment!!!