JAVA SCRIPT - Parsing and Automatically Modifying JSON - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript JAVA SCRIPT - Parsing and Automatically Modifying JSON - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Thursday, January 3, 2019

JAVA SCRIPT - Parsing and Automatically Modifying JSON

 Parsing and Automatically Modifying JSON


Problem

You want to safely create a JavaScript object from JSON. You also want to replace the numeric representation of true and false (1 and 0, respectively) with their Boolean counterparts (true and false).

Solution

Parse the object with the JSON built-in capability added to browsers via ECMAScript 5. To transform the numeric values to their Boolean counterparts, create a reviver function:

var jsonobj = '{"test" : "value1", "test2" : 3.44, "test3" : 0}';
var obj = JSON.parse(jsonobj, function (key, value) {
 if (typeof value == 'number') {
 if (value == 0) {
 value = false;
 } else if (value == 1) {
 value = true;
 }
 }
 return value;
 });
console.log(obj.test3); // false


EXPLAIN

To figure out how to create JSON, think about how you create an object literal and just translate it into a string (with some caveats). If the object is an array:

var arr = new Array("one","two","three");

the JSON notation would be equivalent to the literal notation for the array:

["one","two","three"];


Note the use of double quotes ("") rather than single, which are not allowed in JSON. If you’re working with an object:

var obj3 = {
 prop1 : "test",
 result : true,
 num : 5.44,
 name : "Joe",
 cts : [45,62,13]};


the JSON notation would be:


{"prop1":"test","result":true,"num":5.44,"name":"Joe","cts":[45,62,13]}

Notice in JSON how the property names are in quotes, but the values are only quoted when they’re strings. In addition, if the object contains other objects, such as an array, it’s also transformed into its JSON equivalent. However, the object cannot contain methods. If it does, an error is thrown. JSON works with data only.

The JSON static object isn’t complex, as it only provides two methods: stringify() and parse(). The parse() method takes two arguments: a JSON-formatted string and an optional reviver function. This function takes a key/value pair as parameters, and returns either the original value or a modified result. In the solution, the JSON-formatted string is an object with three properties: a string, a numeric, and a third property, which has a numeric value but is really a Boolean with a numeric representation—0 is false, 1 is true.

To transform all 0, 1 values into false, true, a function is provided as the second ar‐ gument to JSON.parse(). It checks each property of the object to see if it is a numeric. If it is, the function checks to see if the value is 0 or 1. If the value is 0, the return value is set to false; if 1, the return value is set to true; otherwise, the original value is returned.

The ability to transform incoming JSON-formatted data is essential, especially if you’re processing the result of an Ajax request or JSONP response. You can’t always control the structure of the data you get from a service.


No comments:

Post a Comment

Post Top Ad