In this article I will write about working with JSON. JSON stands for JavaScript Object Notation. It is a great way to send objects back and forth through client-server and also to create API’s that return JSON when information is requested. Lets look at an object in javascript.
var object = { self : {name:"customer1",job:"cashier"}, todo :"jsonobject", names : [{employ:"work",happy:true,work:"program"}, {employ:"job",happy:false,work:"intro"}] };
This object, called object has a couple variables and an array names. Now lets see this object in JSON format.
{"self":{"name":"customer1","job":"cashier"}, "todo":"jsonobject", "names":[{"employ":"work","happy":true,"work":"program"}, {"employ":"job","happy":false,"work":"intro"}]}
in javascript to turn an object into JSON you use the JSON.Stringify method
JSON.stringify(object);
And to turn JSON into and Object you use JSON.Parse method
var object = JSON.parse(json);
Now to work with JSON in php you can make objects in php and then use json_encode to turn the object into JSON like this
<?php $myObj = new \stdClass(); $myObj->name = "John"; $myObj->age = 30; $myObj->city = "New York"; $myArray = array(); for($i = 0;$i < 10;$i++){ $myArray[$i] ="hello world".$i; } $myObj->hello = $myArray; $myJSON = json_encode($myObj); echo $myJSON; ?>
Now the variable $myJSON will look like this after you encode it
{"name":"John", "age":30, "city":"New York", "hello":["hello world0","hello world1", "hello world2","hello world3","hello world4", "hello world5","hello world6","hello world7", "hello world8","hello world9"]}
and if you are sending a JSON object to a php script using JSON.stringify you can grab the object in php with something like this using json-decode
$obj = json_decode($_POST["x"], false);
And that is pretty much the basics for using JSON in javascript and php. One note i was looking around and it appears that you are not supposed to send functions through JSON but there is a way that is not consistent so it shouldnt actually be done, as far as i know. I hope you enjoyed this article. Untill next time 🙂
New Amazon Fire HD 8 tablet, 8” HD Display, 3GB memory, 32GB, designed for portable entertainment, Black, (2024 release)
$99.99 (as of February 21, 2025 06:43 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)HP 14 Laptop, Intel Celeron N4020, 4 GB RAM, 64 GB Storage, 14-inch Micro-edge HD Display, Windows 11 Home, Thin & Portable, 4K Graphics, One Year of Microsoft 365 (14-dq0040nr, Snowflake White)
$169.00 (as of February 21, 2025 06:43 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Charger for MacBook Air MacBook Pro 13 14 15 16 inch 2024 2023 2022 2021 2020, M1 M2 M3 M4 Laptop 70W USB C Power Adapter, iPad, LED, 6.6FT USB-C Cable, Charging as Fast as Original Quality
$27.99 (as of February 21, 2025 06:43 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Acer Gateway Chromebook 311 CBO311-1H-C1MX Laptop | Intel Celeron N4500 | 11.6" HD (1366 x 768) Display | 4GB LPDDR4X | 64GB eMMC | Wi-Fi 5 802.11ac | Chrome OS | Star Black
$134.99 (as of February 21, 2025 06:43 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Lenovo IdeaPad 1 Student Laptop Computer, 15.6" FHD Display, Intel Dual Core Processor, 32GB DDR4 RAM, 1TB PCIe SSD, WiFi 6, Bluetooth 5.2, Type-C, Cloud Grey, Windows 11 Pro, Tichang
$379.05 (as of February 21, 2025 06:43 GMT -05:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Author: John Rowan
I am a Senior Android Engineer and I love everything to do with computers. My specialty is Android programming but I actually love to code in any language specifically learning new things.