Saturday, April 21, 2018

About JSON formatter tool

A common use of JSON is to read data from a web server, and display the data in a web page.
JSON formatter


An overview about JSON formatter online tool

JSON Formatter is a web-based tool to view, edit, format, and validate JSON. There are various modes such as a tree editor, a code editor, and a plain text editor. The editor can be used as a component in your own web application. The library can be loaded as CommonJS module, AMD module, or as a regular javascript file. It supported Chrome, Firefox, Safari, Opera, and Internet Explorer 9+.

When cutting and pasting text in the editor using the system clipboard, no data is sent via the server.

This site uses cookies to:

Store your current JSON data at your local PC for convenient usage. You can test by entering to incognito mode in new window or another browser :).
From Google Adsense to personalize ads. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies.

Check this tool to format and validate your JSON data: JSON formatter online

Friday, March 9, 2018

JSON in Javascript

JSON (in Javascript) is a string!
People ofetn assume all Javascript objects is JSON and JSON is a Javascript object. This is incorrect.
In Javascript var x = {x:y} is not JSON, this is a Javascript object. The two are not the same thing. The JSON equivalent (represented in the Javascript language) would be var x = '{"x":"y"}'x is an object of type string not an object in it's own right. To turn this into a fully fledged Javascript object you must first parse it, var x = JSON.parse('{"x":"y"}');x is now an object but this is not JSON anymore.

When working with JSON and JavaScript, you may be tempted to use the eval function to evaluate the result returned in the callback, but this is not suggested since there are two characters (U+2028 & U+2029) valid in JSON but not in JavaScript.
Hence, one must always try to use Crockford's script that checks for a valid JSON before evaluating it.
Below is an example on how to use the JSON Parser (with the json from the above code snippet):
//The callback function that will be executed once data is received from the server
var callback = function (result) {
var johnny = JSON.parse(result);
//Now, the variable 'johnny' is an object that contains all of the properties
//from the above code snippet (the json example)
alert(johnny.firstName + ' ' + johnny.lastName); //Will alert 'John Smith'
};
The JSON Parser also offers another very useful method, stringify. This method accepts a JavaScript object as a parameter, and outputs back a string with JSON format. This would be useful when you want to send data back to the server:
var anObject = {name: "Andreas", surname : "Grech", age : 20};
var jsonFormat = JSON.stringify(anObject);
//The above method will output this: {"name":"Andreas","surname":"Grech","age":20}
The above two methods (parse and stringify) also take a second parameter, which is a function that will be called for every key and value at every level of the final result, and each value will be replaced by result of your inputted function.
Check this to format and validate JSON: JSON formatter online tool.

Friday, January 5, 2018

JSON.stringify()

Convert a JavaScript object into a string with JSON.stringify().

JSON.stringify()

Stringify a JavaScript Object

Imagine we have this object in JavaScript:
var obj = { "name":"John""age":30"city":"New York"};
Use the JavaScript function JSON.stringify() to convert it into a string.
var myJSON = JSON.stringify(obj);
The result will be a string following the JSON notation.
myJSON is now a string, and ready to be sent to a server:

Example

var obj = { "name":"John""age":30"city":"New York"};
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON;
You will learn how to send JSON to the server in the next chapter.

Stringify a JavaScript Array

It is also possible to stringify JavaScript arrays:
Imagine we have this array in JavaScript:
var arr = [ "John""Peter""Sally""Jane" ];
Use the JavaScript function JSON.stringify() to convert it into a string.
var myJSON = JSON.stringify(arr);
The result will be a string following the JSON notation.
myJSON is now a string, and ready to be sent to a server:

Example

var arr = [ "John""Peter""Sally""Jane" ];
var myJSON = JSON.stringify(arr);
document.getElementById("demo").innerHTML = myJSON;

Exceptions

Stringify Dates

In JSON, date objects are not allowed. The JSON.stringify() function will convert any dates into strings.

Example

var obj = "name":"John""today":new Date(), "city":"New York"};
var myJSON = JSON.stringify(obj);

document.getElementById("demo").innerHTML = myJSON;
You can convert the string back into a date object at the receiver.

Stringify Functions

In JSON, functions are not allowed as object values.
The JSON.stringify() function will remove any functions from a JavaScript object, both the key and the value:

Example

var obj = "name":"John""age":function () {return 30;}, "city":"New York"};
var myJSON = JSON.stringify(obj);

document.getElementById("demo").innerHTML = myJSON;
This can be omitted if you convert your functions into strings before running the JSON.stringify() function.

Example

var obj = "name":"John""age":function () {return 30;}, "city":"New York"};
obj.age = obj.age.toString();
var myJSON = JSON.stringify(obj);

document.getElementById("demo").innerHTML = myJSON;
Use this tool to edit, convert, format, and validate JSON: JSON formatter online tool