Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 22, 2021 03:43 pm GMT

Read and write json file with php and mysql

JavaScript Object Notation or JSON is a lightweight data-interchange format which is very easy to read and write, we use JSON to transfer data from server to web-application or vice versa as an alternative to XML. In this post I will be showing you how to read and write JSON file with php and mysql.

What is json? How does it looks?

JSON is nothing but a data format similar like arrays. It comes in key value pair e.g. {Name:Rahul, Age:22}. JSON is a human-readable text format that is completely language independent but uses conventions of programming language like C, C++, JavaScript. See how JSON data looks like.

[
{
"player_name": "Sachin Tendulkar",
"country": "India",
"sports": "Cricket"
},
{
"player_name": "Roger Federer",
"country": "Switzerland",
"sports": "Tennis"
},
{
"player_name": "David Beckham",
"country": "England",
"sports": "Football"
},
]

Read from Json File

I have a file name cricketer.json. Lets parse it and display it in browser. The file content is given below.

[
{
"player_name": "Sachin Tendulkar",
"country": "India",
"sports": "Cricket"
},
{
"player_name": "Roger Federer",
"country": "Switzerland",
"sports": "Tennis"
},
{
"player_name": "David Beckham",
"country": "England",
"sports": "Football"
},
]

To parse JSON data to array check the code below. After parsing, display the data on browser. You can also use this technique for stuff like inserting/updating records to database.

<?php

$string = file_get_contents("file1.json");
$jsonRS = json_decode ($string,true);
foreach ($jsonRS as $rs) {
echo stripslashes($rs["player_name"])." ";
echo stripslashes($rs["country"])." ";
echo stripslashes($rs["sports"])."
";
}
?>

Read More :: https://cmsinstallation.blogspot.com/2019/05/read-and-write-json-file-with-php-and.html


Original Link: https://dev.to/gnral_opll/read-and-write-json-file-with-php-and-mysql-3c0n

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To