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…


This content originally appeared on DEV Community and was authored by koshti Rahul

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. Let’s 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


This content originally appeared on DEV Community and was authored by koshti Rahul


Print Share Comment Cite Upload Translate Updates
APA

koshti Rahul | Sciencx (2021-04-22T15:43:25+00:00) Read and write json file with php and mysql. Retrieved from https://www.scien.cx/2021/04/22/read-and-write-json-file-with-php-and-mysql/

MLA
" » Read and write json file with php and mysql." koshti Rahul | Sciencx - Thursday April 22, 2021, https://www.scien.cx/2021/04/22/read-and-write-json-file-with-php-and-mysql/
HARVARD
koshti Rahul | Sciencx Thursday April 22, 2021 » Read and write json file with php and mysql., viewed ,<https://www.scien.cx/2021/04/22/read-and-write-json-file-with-php-and-mysql/>
VANCOUVER
koshti Rahul | Sciencx - » Read and write json file with php and mysql. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/22/read-and-write-json-file-with-php-and-mysql/
CHICAGO
" » Read and write json file with php and mysql." koshti Rahul | Sciencx - Accessed . https://www.scien.cx/2021/04/22/read-and-write-json-file-with-php-and-mysql/
IEEE
" » Read and write json file with php and mysql." koshti Rahul | Sciencx [Online]. Available: https://www.scien.cx/2021/04/22/read-and-write-json-file-with-php-and-mysql/. [Accessed: ]
rf:citation
» Read and write json file with php and mysql | koshti Rahul | Sciencx | https://www.scien.cx/2021/04/22/read-and-write-json-file-with-php-and-mysql/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.