Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is json,can you explain it to a newbie?

Can you explain it in most simple words?

Best with a demo script.

like image 207
Mask Avatar asked Nov 08 '09 09:11

Mask


People also ask

What is JSON beginner?

JSON stands for JavaScript Object Notation. JSON is a lightweight data-interchange format. JSON is plain text written in JavaScript object notation. JSON is used to send data between computers.

Can you explain about JSON?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

What is JSON explain with example?

JSON stands for Javascript Object Notation. JSON is a text-based data format that is used to store and transfer data. For example, // JSON syntax { "name": "John", "age": 22, "gender": "male", } In JSON, the data are in key/value pairs separated by a comma , . JSON was derived from JavaScript.

What is JSON What is it used for?

It is a text-based way of representing JavaScript object literals, arrays, and scalar data. JSON is relatively easy to read and write, while also easy for software to parse and generate. It is often used for serializing structured data and exchanging it over a network, typically between a server and web applications.


2 Answers

JSON is a way of sharing data (usually between the browser and a server).

JavaScript allows for two way to store collections of values:

//arrays:
[value, value, value]
//objects:
{key:value, key:value, key:value}

At some point, a guru known as Doug realized that it is usually most efficient to send data to JavaScript already setup like an object. [Rather than PHP sending a comma-delimited strings, post-data, XML, or even HTML, all of which have to be painstakingly parsed by the JavaScript].

So he called that idea JSON, wrote up a spec for it, and the standard was born.

For example, let's say your login.php script should return the users name, total posts, and days since registered:

//XML
"<xml..><details>\
    <user>Jim</user><posts>239</posts><since>Jan09</since>\
</details>"
//POSTData
"user=Jim&posts=239&since=Jan09"
//JSON
"{user:'Jim', posts:239, since:'Jan09'}"

The last one can be easily parsed by JS (using eval), and the details can be used intuitively:

var user = details.user;

EDIT:

It was correctly noted that to be valid JSON, all strings must be double quoted. This was done to prevent JS from croaking on reserved keywords (in JS one may not use certain words, such as class, unless they are quoted. So {class:'mike'} cannot be used).

It should also be pointed out that PHP 5.2+ has functions which can be used to create or parse JSON:

<?php
    $arr = array ('a'=>'ay','b'=>'bee','c'=>'cee');
    echo json_encode($arr); //outputs {"a":"ay","b":"bee","c":"cee"}
?>

The json_decode function will ONLY work if the JSON is valid, so it is sometimes important to get those double-quotes right.

like image 74
SamGoody Avatar answered Oct 31 '22 05:10

SamGoody


It's basically a way of describing objects in text - a text-based serialization format. However, the beauty of it is that it's also just normal JavaScript. The syntax of JavaScript allows objects to be initialized in a pretty concise format, and that format is fairly easy to generate/parse in other languages too.

So, you get "built-in" deserialization in JavaScript (i.e. you can just interpret the text as code1) with no extra libraries, and other platforms can create it, usually with a library. (Typically a web server will generate JSON for a browser to interpret.)


1 This assumes you trust your data source completely, of course - executing arbitrary text as code is pretty dangerous from a security standpoint.

like image 40
Jon Skeet Avatar answered Oct 31 '22 04:10

Jon Skeet