Reading Json File Line by Line Php

Read Time: 7 mins Languages:

JSON, short for JavaScript Object Notation, is a common lightweight format for storing and exchanging information. As the proper name suggests, information technology was initially derived from JavaScript, merely it is a language-independent format for storing information. A lot of languages similar PHP at present implement functions to read and create JSON information.

This tutorial will teach you how to read a JSON file and convert it to an array in PHP. Learn how to parse JSON using the json_decode() and json_encode() functions.

Reading JSON From a File or String in PHP

Permit's say you have a file which contains information in JSON format. How exercise you access and store it in PHP?

Kickoff, you need to become the data from the file into a variable by using file_get_contents(). Once the data is in a cord, you can telephone call the json_decode() function to extract information from the cord. Keep in listen that JSON simply provides a style to store information as a string using a set of predefined rules. Information technology is our job to decode the strings properly and get the data we desire.

The json_decode() function accepts four parameters, only you will just need the first two in most situations. The outset parameter specifies the string that you desire to decode. The 2d parameter determines how the decoded information is returned. Setting it to true will return an associative array, and false volition return objects. Here is a basic instance. We have a file chosen people.json with the post-obit contents:

We can read information from this JSON file by using the code below:

In the above example, json_decode() returned an object considering the second parameter was prepare to false. You can prepare it to true to go the data dorsum as an associative array.

At present, we will decode JSON that is slightly more than complicated and try to become back useful information from it.

Our goal is to get back all the countries visited by the person in dissimilar years. The value returned past $decoded_json['countries'] volition actually be an array, and we will loop through information technology like regular arrays to get our data.

Let's get over just i last example of extracting information from a JSON file. Here is the JSON from which we will extract our data.

We accept two nested arrays in the JSON data this time. So we will exist using two nested loops to get the countries visited by different customers.

You should at present have a rough thought of the approach you should accept to read JSON data from a file depending on how it has been created.

Reading JSON Data Without Knowing the Keys Beforehand

So far we take read JSON information where we already knew all the keys. That might not always exist true. Luckily, we tin nonetheless extract useful information from the file once nosotros have stored information technology every bit an associative array. The following example should clear things upwardly.

The keys in the above JSON seem to be random strings that we cannot predict beforehand. However, once we catechumen it into an associative assortment, we will no longer need to know the exact primal values to iterate through the data.

Creating JSON Data in PHP

You tin can too turn your ain data into a well-formatted JSON string in PHP with the aid of the json_encode() function. It basically accepts three parameters, but you will normally only need the first one, i.e. the value y'all desire to encode in most situations.

Yous might also need to utilise some flags in guild to become the JSON cord in the desired format. For instance, y'all can employ the JSON_PRETTY_PRINT flag to add together white space for proper formatting of the JSON string. Similarly, you can use the JSON_PRESERVE_ZERO_FRACTION flag to make sure float values are e'er stored as floats, fifty-fifty if they are equivalent to some integer in magnitude. You can see a list of all such flags in the official documentation.

Dealing With Errors During Encoding and Decoding

The JSON format requires united states to follow a specific set of rules for proper encoding and decoding of the strings. For example, names and values should exist enclosed in double quotes, and there should be no trailing comma after name-value pairs. The json_last_error_msg() role tin can assist y'all figure out what kind of error you are getting and so that you can take appropriate steps. Here is a very bones example:

Final Thoughts

In this tutorial, you learned how to read JSON data from a file or string in PHP. Y'all also learned how to convert that JSON into an array and traverse information technology to extract the data y'all want. Yous should at present be able to become information from JSON in a file where you don't know all the keys in key-value pairs.

In the last two sections, we covered how you tin stringify information as JSON in PHP and the errors you lot might encounter during the encoding and decoding process.

Hopefully, this will answer all your questions about encoding and decoding JSON in PHP.

regaladosolish.blogspot.com

Source: https://code.tutsplus.com/tutorials/how-to-parse-json-in-php--cms-36994

0 Response to "Reading Json File Line by Line Php"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel