×
          
              
          
      
      Clear all filters including search bar
          
        Valeri Tandilashvili's JSON Notes
      
    JSON schema- Describes your existing data format
- Defines the structure of a JSON message
- Clear, human- and machine-readable documentation
- Complete structural validation, useful for automated testing
- Complete structural validation, validating JSON message
- Can be used to validate API request and response$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
// Returns the JSON representation of a value (pretty printed)
echo json_encode($arr, JSON_PRETTY_PRINT);{
    "a": 1,
    "b": 2,
    "c": 3,
    "d": 4,
    "e": 5
}class Emp {
    public $name = "";
    public $hobbies  = [];
    public $birthdate = "";
}
$e = new Emp();
$e->name = "sachin";
$e->hobbies  = ["sport", "reading"];
$e->birthdate = date('Y-m-d', strtotime('2015-11-18'));
echo json_encode($e);{"name":"sachin","hobbies":["sport","reading"],"birthdate":"2015-11-18"}$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
// Decoding into an object
var_dump(json_decode($json));
// Decoding into an array
var_dump(json_decode($json, true));object(stdClass)#1 (5) {
  ["a"]=> int(1)
  ["b"]=> int(2)
  ["c"]=> int(3)
  ["d"]=> int(4)
  ["e"]=> int(5)
}
array(5) {
  ["a"]=> int(1)
  ["b"]=> int(2)
  ["c"]=> int(3)
  ["d"]=> int(4)
  ["e"]=> int(5)
}var text = '{ "name":"John", "age":"function () {return 30;}", "city":"New York"}';
var obj = JSON.parse(text);
obj.age = eval("(" + obj.age + ")");
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age();var text = '{"name":"John", "birth":"1986-12-14", "city":"New York"}';
var obj = JSON.parse(text);
obj.birth = new Date(obj.birth);
document.write(obj.name + ", " + obj.birth)JSON.parse()revivervar text = '{ "name":"John", "birth":"1986-12-14", "city":"New York"}';
var obj = JSON.parse(text, function (key, value) {
  if (key == "birth") {
    return new Date(value);
  } else {
    return value;
  }
});
document.write(obj.name + ", " + obj.birth)JSONP XMLHttpRequest<script>window.onload = function() {
  var s = document.createElement("script");
  s.src = "https://www.w3schools.com/js/demo_jsonp.php";
  document.write(s.outerHTML);
};
// This function will run after the content is fully loaded from the server
function myFunc(myObj) {
  document.write(myObj.name + " is " + myObj.age + " and he lives in " + myObj.city);
} <script>XMLHttpRequestdemo_jsonp.phpmyFunc({"name":"John", "age":30, "city":"New York"});callback functioncallbackURLhttps://api.github.com/users/yui?callback=handleJSONP_variablehandleJSONP_variable({"meta": {
  "Content-Type": "application/javascript; charset=utf-8",
  "Cache-Control": "public, max-age=60, s-maxage=60",
  "Vary": "Accept",
  "status": 200,
  ...
}, "data": {
  "login": "yui",
  "name": "YUI Library",
  "company": null,
  "blog": "http://yuilibrary.com/",
  "location": "Sunnyvale, CA",
  "email": null,
  "public_repos": 31,
  ...
}})stringnumberobjectarraybooleannullfunctionsdatesundefinedsymbolcommentproperty{  
    "employee": {  
        "name": "George",   
        "a0ge": "30",   
        "city": "Tbilisi",   
        "comments": "The best student ever"  
    }  
}var myObj = {
    "name":"John",
    "age":30,
    "cars": {
        "car1":"Ford",
        "car2":"BMW",
        "car3":"Fiat"
    }
}
// Accessing nested object properties
document.write(myObj.cars.car2);
document.write("<br />");
document.write(myObj.cars["car2"]);
document.write("<br />");
document.write(myObj["cars"]["car2"]);<select>document.createElementoptionArray = [
    "daily",
    "weekly",
    "biweekly",
    "monthly"
];
// Creates <select> element
let selector = document.createElement('select');
for(let option in optionArray) {
    let value = optionArray[option];
  
    // Creates <option> element for <select>
    let newOption = document.createElement("option"); 
    
    newOption.value = value;
    newOption.innerHTML = value;
  
    selector.options.add(newOption);
}
// Adds the <select> element into the document object
document.body.appendChild(selector);