Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts

Tuesday 13 February 2018

JSON.stringify - What is JSON.stringify? and how to use?

JSON.stringify - What is JSON.stringify? and how to use?

Question: What is JSON.stringify()?
The JSON.stringify() method converts a JavaScript value to a JSON string.
It can also convert a Object/Array to JSON String.


Question: Give few example of JSON.stringify()?
Following are few example of JSON.stringify()
JSON.stringify({});                  // {} 


JSON.stringify(true);                // 'true'


JSON.stringify('foo');               // '"foo"'


JSON.stringify([1, 'false', false]); // '[1,"false",false]'


JSON.stringify({ x: 5 });            // '{"x":5}'


JSON.stringify({ x: 5, y: 6 });// '{"x":5,"y":6}'


JSON.stringify({ x: 5, y: 6 });// '{"x":5,"y":6}'



Question: What is use of 2nd parameter of JSON.stringify()?
2nd parameter of JSON.stringify() is a replacer.
It can be an array OR can be function.




Question: Give an example of callback function of replacer of JSON.stringify()?
var objectData={}
objectData.a='Apple';
objectData.b=undefined;
objectData.c=';
   
objectData=JSON.stringify(objectData, function(k, v) {                
if (typeof v === 'undefined') {
  return '';
}else{
    return v;
}
});

this replacer function will replace the undefined with empty space.




Question: Give an example of array of replacer of JSON.stringify()?
var foo = {name: 'web tech', week: 350, transport: 'car', month: 7};
JSON.stringify(foo, ['week', 'month']);  //"{"week":350,"month":7}"

IT will return those which match with array




Question: Difference between JSON.stringify and JSON.parse?
JSON.stringify convert a Javascript object into JSON text(JOSN string).
JSON.parse convert a JSON text into into Javascript object.

Both are opposite to each other.

Tuesday 22 March 2016

How to decode json string in PHP?

How to decode json string in PHP?

Question: Give an Sample of Correct JSON Object?
[{"name":"PHP ","Description":"Web technology experts notes"},{"name":"JSON","Description":"JSON Interview Questions"}]



Question: Can we check, JSON is valid OR Not?
Yes, we can do.
Below function check that JSON is valid or Not. (1 for valid, 0 for invalid)
function isValidJson($string) {
 json_decode($string);
 return (json_last_error() == JSON_ERROR_NONE);
}



Question: How to check JSON is valid?
$string='[{"name":"PHP ","Description":"Web technology experts notes"},{"name":"JSON","Description":"JSON Interview Questions"}]';
if(isValidJson($string)){
    echo "JSON is Valid"; //This will print
}else{
    echo "JSON is NOT Valid";
}



Question: How to check JSON is NOT valid?
$string='[{{{{{"name":"PHP ","Description":"Web technology experts notes"},{"name":"JSON","Description":"JSON Interview Questions"}]';
if(isValidJson($string)){
    echo "JSON is Valid"; 
}else{
    echo "JSON is NOT Valid";//This will print
}



Question: How to check JSON is empty OR Not?
$string='{}';
$data = json_decode($string);
if(empty($data) || count($data)){
    echo "JSON is empty";
}else{
    echo "JSON is NOT empty";
}



Question: How to create an empty JSON?
$string='{}';



Question: How to decode JSON string in PHP?
$string='[{"name":"PHP ","Description":"Web technology experts notes"},{"name":"JSON","Description":"JSON Interview Questions"}]';
$data = json_decode($string);



Question: How to create JSON from php array?
$array = array('name'=>'PHP Tutorial','Description'=>'Web technology experts notes');
echo json_encode($array);



Question: What is the Correct content-type of JSON?
application/json


Question: How to convert JSON Object into PHP Object?
$string='[{"name":"PHP ","Description":"Web technology experts notes"},{"name":"JSON","Description":"JSON Interview Questions"}]';
$data = json_decode($string,false);
print_r($data);



Question: How to convert JSON Object into PHP Array?
$string='[{"name":"PHP ","Description":"Web technology experts notes"},{"name":"JSON","Description":"JSON Interview Questions"}]';
$data = json_decode($string,true);
print_r($data);



Question: How to get JSON error?
$string='Invalid JSON';
json_decode($string);
echo json_last_error(); //4



Question: How to get JSON error Message?
$string='Invalid JSON';
json_decode($string);
echo json_last_error_msg (); //Syntax error



Friday 20 March 2015

What is the Correct content-type of JSON and JSONP?

What is the Correct content-type of JSON and JSONP?


Question: What is the Correct content-type of JSON?
  • application/json
  • application/javascript
  • application/x-javascript
  • text/javascript
  • text/x-javascript
  • text/x-json

Answer:
application/json

JSON is a domain-specific language (DSL) and a data format is independent of JavaScript it have its own MIME type i.e. application/json.

Example of JSON:
{ "Name": "Web Technology", "Website": "http://www.web-technology-experts-notes.in" }

Content-Type: application/json




Question: What is the Correct  content-type of JSONP?
  • application/json
  • application/x-javascript
  • application/javascript
  • text/javascript
  • text/x-javascript
  • text/x-json
Answer:
application/javascript

JSONP is JSON with padding. Response is JSON data but with a function call wrapped around it.

Example of JSONP:
functioncallExample({ "Name": "Web Technology", "Website": "http://www.web-technology-experts-notes.in" });

Content-Type: application/javascript



Tuesday 19 August 2014

Json Interview Questions And Answers For Freshers

Json Interview Questions And Answers For Freshers

Question: What is full Form JSON?
Answer: JavaScript Object Notation.


Question: What is JSON?
Answer: It is text-based standard design for interchange the information Between two or more different application. It is also very light as compare to XML and human readable.


Question: Who created the JSON?
Answer: Douglas Crockford


Question: Who is Father of JSON?
Answer: Douglas Crockford


Question: Why It is so popular?
Answer:
  1. It is light weight standard for exchange information
  2. It is independent of language
  3. It is independent of Hosting Server
  4. Human Readable
  5. Easy to Encode
  6. Easy to Decode
  7. Used in Mobile application



Question: Where JSON is used?
Answer: It is used to transfer the information between two application. Information can also be exchanged between two different application which is running on different OR same server. It is used in Web Application, Mobile Application (IOS, Android, Iphone, window Phone)


Question: What is file extension of JSON?
Answer: .json


Question: Why Use JSON over XML?
Answer: 

  1. It is light weight standard for exchange information.
  2. Human readable
  3. Easy to parse in PHP as well as in javaScript.
  4. Due to light weight It is fast.
  5. Question: Explain JSON Structures?
  6. Answer: It as an object, structure, or associative array where we stored data an ordered list.
  7. Each value have an key
  8. once key may have an further object
  9. Its lightweight that's why used in android application
  10. A JSON object begins with { and ends with } (braces)
  11. Each name is followed by : (colon) and the name/value pairs are separated by , (comma)


Question: Which Browser Support JSON?
Answer: JSON support is included in newer browsers and in the newest ECMAScript (JavaScript) standard.

  • Firefox (Mozilla) 3.5
  • Internet Explorer 8
  • Chrome
  • Opera 10
  • Safari 4



Question: What is JSON Parser?
Answer:JSON Parser is used to parse the JSON data into object to use its value. JSON can be parse by javaScript, jQuery and PHP.


Question: What is JSON-RPC?
Answer: Remote Procedure call protocol with use of JSON. It is similar to XML-RPC only difference, It use JSON instead of XML.

Question: What is JSON-RPC-Java?
Answer: JSON-RPC-Java is a Java implementation of the JSON-RPC protocol.


Question: How to create json from php array?
Answer:
$array = array('name'=>'PHP Tutorial','Description'=>'Web technology experts notes');
echo json_encode($array);


Question: How to get PHP array from json Object?
Answer:
$object='{"name":"PHP Tutorial","Description":"Expert in web technology"}';
$array = json_decode($object);


Question: How to parse JSON in JavaScript?
Answer:
var json = '{"name":"PHP Tutorial","Description":"Web technology experts notes"}'
    var obj = JSON.parse(json);
    //alert(obj.name);


Question: How to create JSON Object from JavaScript?
Answer:
var obj = {};
    obj['name']='php-tutorial-php.blogspot.in'; //string
    obj['age'] = 32;  // integer.
    obj['marks']= [80,70,60,50,60,80]; //array


Question: How to parse JSON in jQuery?
Answer:
var json = '{"name":"PHP Tutorial","Description":"Web technology experts notes"}'
    obj = $.parseJSON(json);
    //alert(obj.name);


Question: How to Validate json in php?
Answer:
$json = '{"name":"PHP Tutorial","Description":"Web technology experts notes"}';
$obj = json_decode($json);
if(is_null($obj)) {
 die('Invalid JSON');
}


Question: How to Validate json in javascript?
Answer:
function isValidJson(jsonData) {
    try {
        JSON.parse(jsonData);
        return true;
    } catch (e) {
        return false;
    }
}
var json = '{"name":"PHP Tutorial","Description":"Web technology experts notes"}'
isValidJson(json);


Question: How to Validate json in jQuery?
Answer:
 function isValidJson(jsonData) {
    try {
        $.parseJSON(jsonData);
        return true;
    } catch (e) {
        return false;
    }
}
var json = '{"name":"PHP Tutorial","Description":"Web technology experts notes"}'
isValidJson(json);


Question: How Convert json object to json string in jquery?
Answer:
var json = '{"name":"PHP Tutorial","Description":"Web technology experts notes"}'
JSON.stringify(json)


Question: How to convert json object to json string in javaScript?
Answer:
var json = '{"name":"PHP Tutorial","Description":"Web technology experts notes"}'
JSON.stringify(json)


Question: How to receive JSON Data at the Client Side?
Answer:
var json = '{"name":"PHP Tutorial","Description":"Web technology experts notes"}'
obj = $.parseJSON(json)


Question: How to get JSON response in Ajax?
Answer:
$.ajax({
  dataType: "json",
  url: '/ajax/url',
  data: 'name=php-tutorial-php',
  success: function(data){
  //data
  }
});


Question: What are online tool for Validate JSON data?
Answer:
http://jsonlint.com
http://www.freeformatter.com/json-validator.html


Question: What are JSON online tool for Formatter & Validator?
Answer: http://jsonlint.com/
http://www.freeformatter.com/json-validator.html
http://jsonviewer.stack.hu/
http://json.parser.online.fr/


Tuesday 2 July 2013

Difference between JSON and JSONP

Difference between JSON and JSONP

Full Form of JSON
JavaScript Object Notation

Full Form of JSONP
JavaScript Object Notation with Padding.



Json is stardard format that is human readable used to transmit information from one server to another server.
Jsonp is a json with ability to transmit information to another domain.



JSONP is JSON with padding, that is, you put a string at the beginning and a pair of parenthesis around it. 

JSON Example
{"Name": "Foo", "Id": 1234, "Rank": 7};

JSONP Example - In this you can call a function
functionCall({"Name": "Foo", "Id": 1234, "Rank": 7});




With JSON you can't send request to another domain whereas JSONP is really a simply trick to overcome XMLHttpRequest same domain policy. In JSONP you can send request to a different domain.) 
Demo with JSONP
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
        <script>
            $(document).ready(function(){
                $.ajax({
                    url: 'http://twitter.com/status/user_timeline/padraicb.json?count=5',
                    dataType: 'jsonp',
                    success: function(dataWeGotViaJsonp){
                        var htmlData = '';
                        var len = dataWeGotViaJsonp.length;
                        for(var i=0;i<len;i++){
                            twitterEntry = dataWeGotViaJsonp[i];
                            htmlData += '<p>
<img src = "' + twitterEntry.user.profile_image_url_https +'"/>' + twitterEntry['text'] + '</p>
'
                        }
                        $('#MyTwitterFeed').html(htmlData);
                    }
                });
            })
        </script>
        <div id="MyTwitterFeed">
</div>