Как создать файл json php
Перейти к содержимому

Как создать файл json php

  • автор:

Как создать файл json php

(opens new window) ) is a platform and language independent way of serializing objects into plaintext. Because it is often used on web and so is PHP, there is a basic extension

(opens new window) for working with JSON in PHP.

# Decoding a JSON string

(opens new window) function takes a JSON-encoded string as its first parameter and parses it into a PHP variable.

Normally, json_decode() will return an object of \stdClass

(opens new window) if the top level item in the JSON object is a dictionary or an indexed array if the JSON object is an array. It will also return scalar values or NULL for certain scalar values, such as simple strings, "true" , "false" , and "null" . It also returns NULL on any error.

(opens new window) to view the types and values of each property on the object we decoded above.

Output (note the variable types):

Note: The variable types in JSON were converted to their PHP equivalent.

(opens new window) for JSON objects instead of returning an object, pass true as the second parameter

Output (note the array associative structure):

The second parameter ( $assoc ) has no effect if the variable to be returned is not an object.

Note: If you use the $assoc parameter, you will lose the distinction between an empty array and an empty object. This means that running json_encode() on your decoded output again, will result in a different JSON structure.

If the JSON string has a "depth" more than 512 elements (20 elements in versions older than 5.2.3, or 128 in version 5.2.3) in recursion, the function json_decode() returns NULL . In versions 5.3 or later, this limit can be controlled using the third parameter ( $depth ), as discussed below.

According to the manual:

PHP implements a superset of JSON as specified in the original [» RFC 4627](http://www.faqs.org/rfcs/rfc4627) — it will also encode and decode scalar types and NULL. RFC 4627 only supports these values when they are nested inside an array or an object. Although this superset is consistent with the expanded definition of "JSON text" in the newer [» RFC 7159](http://www.faqs.org/rfcs/rfc7159) (which aims to supersede RFC 4627) and [» ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf), this may cause interoperability issues with older JSON parsers that adhere strictly to RFC 4627 when encoding a single scalar value.

This means, that, for example, a simple string will be considered to be a valid JSON object in PHP:

But simple strings, not in an array or object, are not part of the RFC 4627

(opens new window) standard. As a result, such online checkers as JSLint

(opens new window) (in RFC 4627 mode) will give you an error.

There is a third $depth parameter for the depth of recursion (the default value is 512 ), which means the amount of nested objects inside the original object to be decoded.

There is a fourth $options parameter. It currently accepts only one value, JSON_BIGINT_AS_STRING . The default behavior (which leaves off this option) is to cast large integers to floats instead of strings.

Invalid non-lowercased variants of the true, false and null literals are no longer accepted as valid input.

So this example:

Similar behavior occurs for false and null .

Note that json_decode() will return NULL if the string cannot be converted.

It is not safe to rely only on the return value being NULL to detect errors. For example, if the JSON string contains nothing but "null" , json_decode() will return null , even though no error occurred.

# Encoding a JSON string

(opens new window) function will convert a PHP array (or, since PHP 5.4, an object which implements the JsonSerializable interface) to a JSON-encoded string. It returns a JSON-encoded string on success or FALSE on failure.

During encoding, the PHP data types string, integer, and boolean are converted to their JSON equivalent. Associative arrays are encoded as JSON objects, and – when called with default arguments – indexed arrays are encoded as JSON arrays. (Unless the array keys are not a continuous numeric sequence starting from 0, in which case the array will be encoded as a JSON object.)

# Arguments

Since PHP 5.3, the second argument to json_encode is a bitmask which can be one or more of the following.

As with any bitmask, they can be combined with the binary OR operator | .

Read, Decode, Encode, Write JSON in PHP

Nico Dupont

JSON (JavaScript Object Notation) is a standard file format for data exchange between applications.

In this tutorial, we will learn how to perform the most common manipulation, such as encoding, decoding, and converting JSON to Array and Array to JSON with the help of examples.

We'll also discover how to read and write JSON files in PHP. Finally, we'll look at how to stream a big JSON file while using very little memory.

The whole code and examples are available in a GitHub repository; it comes packaged as a simple Symfony project and provides a Docker image.

JSON String Example​

In this tutorial, we'll use the following JSON string containing a list of movies and their properties.

Convert a JSON String to an Associative Array Using json_decode​

We use json_decode to convert (decode) the string to a PHP associative array.

The second parameter of json_decode specifies if we want to get each JSON object as an associative array or as an object. Here, we go for the associative array option.

The result is an array containing one associative array for each movie. The keys of each associative array are the fields of the JSON object.

Convert an Associative Array to a JSON string Using json_encode​

We use json_encode to convert (encode) the associative array to a JSON string.

The second parameter of json_encode specifies the format of the string. We can go for the compact option by default. We can also use the pretty format; it includes line breaks and spaces to make the result more human-readable.

Как создать файл json php

In this article, we are going to generate a JSON file in PHP by using an array. JSON stands for JavaScript object notation, which is used for storing and exchanging data. JSON is text, written with JavaScript object notation.




  1. JSON doesn’t use an end tag
  2. It is shorter.
  3. It is quicker to read and write.
  4. It can use arrays.

Approach: In this article, we can generate JSON data using an array., create an array



Use json_encode() to convert array to JSON. It is used to convert array to JSON


Example: Place the file in the path using file_put_contents()

The file_name is the JSON to be saved and json_object is the object after JSON from the array is created.

Generate JSON File in PHP

Generate JSON File in PHP

In this article, we will introduce the method to generate a .json file in PHP.

Please enable JavaScript

  • Using file_put_contents() function

Use file_put_contents() Function to Generate a .Json File in PHP

The built-in function file_put_contents() could write the content into a file in PHP. It searches for the file to write in, and if the desired file is not present, it creates a new file. We can use this function to create a .json file. The correct syntax to use this function is as follows

This function returns the number of bytes written on the file if successful and false otherwise.

The below program will create a new .json file and store JSON data into it

We use json_encode() function to convert the data stored in the array to a JSON string. Once the data is converted to a JSON string, file_put_contents() function creates a .json file and writes data into it. The output shows the number of bytes, which means the data is written successfully.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *