Friday, May 26, 2017

Differences between JSON and JSONP

What are the differences between JSON and JSONP? Are they the same? Let's find out.

JSON vs JSONP

JSON is a language designed for lightweight data exchange in an format that is easy for both humans and computers to understand. See: JSON formatter for more information.
JSON language could work on a technology which is not restricted by the same origin policy with only a small amount of changes required. The combination of this tech, along with the adapted form of JSON was branded JSONP.
The new technology that is fundamental to JSONP is nothing more than the bog-standard <script> tag. 
The full power of the <script> tag is often overlooked, or at the very least, taken for granted:
  1. <script> tags are not restricted by any same origin policy, so can make requests to any domain.
  2. <script> tags can be added programatically, which allows developers to request resources via the <script> tag as and when required:
    var element = document.createElement("script"); // create the element
    element.src = 'http://somewhere.com/some/resource.js'; // set the target resource
    document.getElementsByTagName("head")[0].appendChild(element); // add the element to the DOM
  3. The src attribute of the <script> tag is not restricted to just JavaScript files. Whatever the target, it will be retrieved via the relevant transport protocol (HTTP GET/ HTTPS GET), and the content will be evaluated by the JavaScript engine. If the content is not valid JavaScript, a Parse Error will be thrown.
JSONP is JSON with padding. You put a string at the beginning and a pair of parenthesis around it. For example:



//JSON
{"name":"stackoverflow","id":5}
//JSONP
func({"name":"stackoverflow","id":5});

Thursday, May 18, 2017

JSON Arrays

Arrays as JSON Objects

Example

"Ford""BMW""Fiat" ]
As you can learn from JSON formatter, Arrays in JSON are almost the same as in JavaScript.
Array values in JSON need to be of type string, number, object, array, boolean, or null.
Array values in Javascript can be all of the above and any other valid JavaScript expression such as functions, dates, and undefined.

Arrays in JSON Objects

Arrays can be values of an object property:
{
"name":"John",
"age":30,
"cars":[ "Ford""BMW""Fiat" ]
}

Access Array Values

Access the array values by using the index number:
x = myObj.cars[0];

LoopThrough an Array

Access array values by using a for-in loop:
for (i in myObj.cars) {
    += myObj.cars[i];
}
Or use a for loop:
for (i 0; i < myObj.cars.length; i++) {
    x += myObj.cars[i];
}


Nested Arrays in JSON Objects

Values in an array can also be another array, or even another JSON object:
myObj = {
    "name":"John",
    "age":30,
    "cars": [
        { "name":"Ford""models":[ "Fiesta""Focus""Mustang" ] },
        "name":"BMW""models":[ "320""X3""X5" ] },
        "name":"Fiat""models":[ "500""Panda" ] }
    ]
 }
Use a for-in loop for each array in order to access arrays inside arrays:
for (i in myObj.cars) {
    x += "<h1>" + myObj.cars[i].name "</h1>";
    for (j in myObj.cars[i].models) {
        x += myObj.cars[i].models[j];
    }
}

Modify Array Values

Use the index number to modify an array:
 myObj.cars[1] = "Mercedes";

Delete Array Items

Use the delete keyword to delete items from an array:
delete myObj.cars[1];

Friday, May 12, 2017

JSON Parse

JSON is often used to exchange data to/from a web server. The data is always a string when you receive it from a web server. Parse the data with JSON.parse() to make it becomes a Javascript object. This site will make it easier for you if you want to get JSON formatter

PARSING JSON - EXAMPLE

Imagine we received this text from a web server:
'{ "name":"John", "age":30, "city":"New York"}'
Use the JavaScript function JSON.parse() in order to convert text into a JavaScript object:
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
Reember to make sure that the text is written in JSON format, otherwise, you will receive  a syntax error.
Use the JavaScript object in your page:

Example

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age
</script>