JSON

From BC$ MobileTV Wiki
Jump to: navigation, search

JavaScript Object Notation (JSON)

JavaScript Object Notation (commonly abbreviated JSON) is as it name suggests, a JavaScript syntax-based markup for providing dynamic data access to web pages & web applications (often used in session-based accesses to unique data via API). With JSON a session can range from the lifetime of a single (HTTP) request, or each time it a page is loaded - to actually storing the JSON longer term in a traditional SQL database as text or a JSON Blob, or, even purely JSON-based or NoSQL databases like MongoDB. JSON is particular useful for building AJAX type of functionality, but with an even simpler to parse data model in native JavaScript. The syntax and formatting rules were designed to specify a lightweight data-interchange format that is easy for humans to both read and write. As a core design goal, it is equally important for this format to be easy for machines to parse and generate as well, though.

PROS

Advocates of JSON claim it has most of the expressiveness of XML without the overhead that well-formedness of elements (redundant tag closures), comments and/or floating CDATA blocks all tend to bring. Another commonly praised characteristic is the ability to circumvent the same-origin policy when processing JSON data via JSONP (although JSONp as an approach itself has recently come under criticism for its own security vulnerabilities), however circumventing same-origin is impossible to do when using XML or other data formats without use of a server-side proxy or other hacks/tricks that may pose an even greater security risk that requires strict whitelist/blacklist and other security approaches to close off.

CONS

Critics of JSON have often cited its lack of server-side support and limitation to browser-based applications, which is increasingly less of an issue as many native language implementations and extensions emerge; they also note the inability to represent cyclical and/or referential data structures as a shortcoming, which library implementations[1] as well as RDF/JSON (in response to inability to represent RDF) and similar initatives are trying to solve. Finally, an often debated subject is the safety of eval commonly used in processing JSON, and the potential security issues that can arise through the use of JSON in cross-domain requests (via JSONP). In the words of JSON's founder "eval is evil" and there are alternative approaches that don't rely on this, such as native parsing libraries (JSON is now native to most browsers) and HTML5 cross-domain messaging.








Specifications

JSON

JSONP

See dedicated section: JSONp [2]

JSON Schema

Similar to XML Schema, JSON Schema proposes a structured data definition language for expressing allowed/expected elements of a JSON Object.

JSONPath

[3][4][5][6][7][8][9][10][11]

JSON-RPC

SOAPjr

RDF/JSON

There are a few different approaches to serializing RDF as JSON (or, mapping JSON into RDF). None have been formally standardized as yet, but some of the ongoing efforts include:


JSON-LD


Language

Derivation

It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.


Structure

JSON is built on two structures:

   * A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
   * An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangable with programming languages also be based on these structures.

In JSON, they take on these forms:

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).



Grammar

object

   {}
   { members } 

members

   pair
   pair , members

pair

   string : value

array

   []
   [ elements ]

elements

   value
   value , elements

value

   string
   number
   object
   array
   true
   false
   null

string

   ""
   " chars "

chars

   char
   char chars

char

   any-Unicode-character-
       except-"-or-\-or-
       control-character
   \"
   \\
   \/
   \b
   \f
   \n
   \r
   \t
   \u four-hex-digits 

number

   int
   int frac
   int exp
   int frac exp 

int

   digit
   digit1-9 digits
   - digit
   - digit1-9 digits 

frac

   . digits

exp

   e digits

digits

   digit
   digit digits

e

   e
   e+
   e-
   E
   E+
   E-

[16]


Security




EXAMPLES

Example 1

{
   "message":
            {
               "service":
                         {
                            "id": "BMI",
                            "endpoint": "/bmi/bus-interface/savoir-wrapper.php?bmi=24&height=1.75&weight=75&mode=json",
                            "parameter":
                                       [
                                         {"id": "bmi", "value": "24"},
                                         {"id": "height", "value": "179", "unit": "cm", "unit_type": "LENGTH"},
                                         {"id": "weight", "value": "88", "unit": "kg", "unit_type": "MASS"},
                                       ]
                         }
            }
}


(Equivalent XML):

<message>
  <service id="BMI" endpoint="/bmi/bus-interface/savoir-wrapper.php">
    <parameter id="bmi" value="27.5"/>
    <parameter name="height" value="179" unit="cm" unit_type="LENGTH"/>
    <parameter name="weight" value="88" unit="kg" unit_type="MASS"/>
  </service>
</message>


Example 2

{
    "responseData" : {
        "results" : [{
            "GsearchResultClass" : "GwebSearch",
            "unescapedUrl" : "http://www.wireless.att.com/learn/messaging-internet/mobile-tv/",
            "url" : "http://www.wireless.att.com/learn/messaging-internet/mobile-tv/",
            "visibleUrl" : "www.wireless.att.com",
            "cacheUrl" : "http://www.google.com/search?q\u003dcache:eBCoR4tgM24J:www.wireless.att.com",
            "title" : "AT\u0026amp;T \u003cb\u003eMobile TV\u003c/b\u003e®",
            "titleNoFormatting" : "AT\u0026amp;T Mobile TV®",
            "content" : "Tune in anytime 24/7 for prime time TV with \u003cb\u003eMobile TV\u003c/b\u003e from AT\u0026amp;T."
        },
        {
            "GsearchResultClass" : "GwebSearch",
            "unescapedUrl" : "http://en.wikipedia.org/wiki/Mobile_television",
            "url" : "http://en.wikipedia.org/wiki/Mobile_television",
            "visibleUrl" : "en.wikipedia.org",
            "cacheUrl" : "http://www.google.com/search?q\u003dcache:dEPdCpJUClkJ:en.wikipedia.org",
            "title" : "\u003cb\u003eMobile\u003c/b\u003e television - Wikipedia, the free encyclopedia",
            "titleNoFormatting" : "Mobile television - Wikipedia, the free encyclopedia",
            "content" : "\u003cb\u003eMobile TV\u003c/b\u003e is one of the features provided by many 3G phones. In 2005, South   Korea became the first country in the world to have \u003cb\u003emobile TV\u003c/b\u003e when it started   \u003cb\u003e...\u003c/b\u003e"
        }],
        "cursor" : {
            "pages" : [{
                "start" : "0",
                "label" : 1
            },
            {
                "start" : "8",
                "label" : 2
            },
            {
                "start" : "16",
                "label" : 3
            },
            {
                "start" : "24",
                "label" : 4
            },
            {
                "start" : "32",
                "label" : 5
            },
            {
                "start" : "40",
                "label" : 6
            },
            {
                "start" : "48",
                "label" : 7
            },
            {
                "start" : "56",
                "label" : 8
            }],
            "estimatedResultCount" : "65600000",
            "currentPageIndex" : 0,
            "moreResultsUrl" : "http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dMobile+TV"
        }
    },
    "responseDetails" : null,
    "responseStatus" : 200
}

Parsing the results in JavaScript (with the evil of eval):

 <script>
   var json =  eval('(' + jsonText + ')');
   result = json.responseData.results[1].url;
   document.write(result);
 </script>

Parsing the results in JavaScript (without eval, using json2.js by Douglas Crockford)

 <script type="text/javascript" src="https://github.com/douglascrockford/JSON-js/raw/master/json2.js"></script>
 <script>
   var json = JSON.parse(jsonText);
   result = json.responseData.results[1].url; 
 </script>

[17]




Tools


JQ


Validator


Editor


Converter

[20]


Parsers

JavaScript

jQuery

Java

PHP


Python


C

C++

C#

Objective-C

XSLT

Flash/Flex


Resources


Tutorials


External Links


References

  1. cycle.js - support for cyclical JSON data strcutures in JS: https://github.com/douglascrockford/JSON-js/blob/master/cycle.js
  2. What is JSONp: http://remysharp.com/2007/10/08/what-is-jsonp/
  3. JSONpath - original GoogleCode project: https://code.google.com/archive/p/json-path/ | Alternative initial implementation
  4. JSONPath implementation: https://developer.gnome.org/json-glib/unstable/JsonPath.html
  5. Java JsonPath implementation: https://github.com/jayway/JsonPath
  6. Node.JS -- JSONPath: https://www.npmjs.com/package/JSONPath
  7. Apache Camel - JSONpath: https://camel.apache.org/jsonpath.html
  8. PHP - JSONpath: https://github.com/Skyscanner/JsonPath-PHP
  9. Python - JSONpath: https://pypi.python.org/pypi/jsonpath-rw
  10. Working with JSONPath and PHP: http://www.w3resource.com/JSON/JSONPath-with-PHP.php
  11. Is there a JSON equivalent of XQuery/XPath?: http://stackoverflow.com/questions/8481380/is-there-a-json-equivalent-of-xquery-xpath
  12. JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios: https://www.blazemeter.com/blog/advanced-usage-json-path-extractor-jmeter
  13. New W3C RDF working group to tackle API, JSON serialization: http://www.w3.org/QA/2010/12/new_rdf_working_group_rdfjson.html
  14. Serializing SPARQL Query Results in JSON: http://www.w3.org/TR/rdf-sparql-json-res/
  15. From JSON to RDF in Six Easy Steps with JRON: http://decentralyze.com/2010/06/04/from-json-to-rdf-in-six-easy-steps-with-jron/
  16. JSON.org: http://json.org
  17. Paste into Try-it-now to see the second search Google result for the term 'Mobile TV': http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_date_weekday
  18. What is Wrong with JSON Viewer on Codeplex: http://chuchuva.com/pavel/2016/07/what-is-wrong-with-json-viewer-on-codeplex/
  19. Parsing JSON with jq : http://www.compciv.org/recipes/cli/jq-for-parsing-json/
  20. PrettyPrint.js (variable dumper): https://github.com/padolsey-archive/prettyprint.js
  21. Performance Comparison of JSON Libraries -- Jackson vs gson vs fastjson vs JSON.simple vs JSONP: https://interviewbubble.com/performance-comparison-of-json-libraries-jackson-vs-gson-vs-fastjson-vs-json-simple-vs-jsonp/
  22. Comparing the JSON APIs Gson and Jackson : https://dzone.com/articles/compare-json-api
  23. JAXB vs. GSON and Jackson: https://tuhrig.de/jaxb-vs-gson-and-jackson/
  24. Jackson Vs. GSON: https://stackoverflow.com/questions/2378402/jackson-vs-gson
  25. GSON vs Jackson -- Which to Use for JSON in Java: http://www.doublecloud.org/2015/03/gson-vs-jackson-which-to-use-for-json-in-java/
  26. Jackson vs GSON: https://www.baeldung.com/jackson-vs-gson
  27. How to read json file into java with simple JSON library: https://stackoverflow.com/questions/10926353/how-to-read-json-file-into-java-with-simple-json-library
  28. JSON.simple example – Read and write JSON: https://www.mkyong.com/java/json-simple-example-read-and-write-json/
  29. Gson User Guide: https://sites.google.com/site/gson/gson-user-guide | GSON - Array examples
  30. GSON Cookbooks and Examples: https://github.com/eugenp/tutorials/tree/master/gson
  31. How to convert Java object to / from JSON (Gson): https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
  32. Converting JSON data to Java object: https://stackoverflow.com/questions/1688099/converting-json-data-to-java-object/1688182#1688182
  33. Gson Example Tutorial Parse JSON: https://www.journaldev.com/2321/gson-example-tutorial-parse-json
  34. GSON - JsonParser: http://tutorials.jenkov.com/java-json/gson-jsonparser.html
  35. Gson — Mapping of Nested Objects: https://futurestud.io/tutorials/gson-mapping-of-nested-objects
  36. Gson — Mapping of Arrays and Lists of Objects: https://futurestud.io/tutorials/gson-mapping-of-arrays-and-lists-of-objects
  37. Using Gson to parse Json array and object with no name: https://stackoverflow.com/questions/13736122/using-gson-to-parse-json-array-and-object-with-no-name
  38. Using GSON to parse a JSON array: https://stackoverflow.com/questions/18421674/using-gson-to-parse-a-json-array/18421994
  39. JSON Manipulation Using GSON: https://dzone.com/articles/json-manipulation-using-gson
  40. Jackson DataBind: https://github.com/FasterXML/jackson-databind
  41. Jackson Cookbooks & Examples: https://github.com/eugenp/tutorials/tree/master/jackson
  42. Jackson 2 – Convert Java Object to / from JSON: https://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/
  43. Jackson Streaming API to read and write JSON: https://www.mkyong.com/java/jackson-streaming-api-to-read-and-write-json/
  44. Processing JSON with Jackson: https://dzone.com/articles/processing-json-with-jackson
  45. Jackson ObjectMapper tutorial: https://www.baeldung.com/jackson-object-mapper-tutorial
  46. Getting at nested values with Jackson JSON lib: http://www.baeldung.com/jackson-nested-values
  47. Intro to the Jackson ObjectMapper: http://www.baeldung.com/jackson-object-mapper-tutorial
  48. Jackson Annotation examples: https://www.baeldung.com/jackson-annotations
  49. Can not deserialize instance of java.util.HashMap out of START_ARRAY token: https://stackoverflow.com/questions/36519974/can-not-deserialize-instance-of-java-util-hashmap-out-of-start-array-token
  50. Dump object to String with Jackson: https://stackoverflow.com/questions/9767131/dump-object-to-string-with-jackson
  51. Convert JSON String to JsonNode (wihtout intermediary POJO/ObjectMapper) using Jackson: https://attacomsian.com/blog/jackson-convert-json-string-to-json-node
  52. How to convert Java object to / from JSON (Jackson): https://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
  53. Jackson JSON Java Parser API Example Tutorial: https://www.journaldev.com/2324/jackson-json-java-parser-api-example-tutorial
  54. Converting Java objects to JSON with Jackson: https://stackoverflow.com/questions/15786129/converting-java-objects-to-json-with-jackson
  55. Jackson ObjectMapper: http://tutorials.jenkov.com/java-json/jackson-objectmapper.html
  56. Jackson Annotations for JSON (Part 1) -- Serialization & Deserialization: https://dzone.com/articles/jackson-annotations-for-json-part-1-serialization
  57. Processing JSON With Jackson: https://dzone.com/articles/processing-json-with-jackson
  58. Map Serialization and Deserialization with Jackson: http://www.baeldung.com/jackson-map
  59. Jackson annotations: https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations
  60. Jackson Date -- Timestamps/Dates/String formatting output of JSON Dates: http://www.baeldung.com/jackson-serialize-dates
  61. Ignoring new fields on JSON objects using Jackson: https://stackoverflow.com/questions/5455014/ignoring-new-fields-on-json-objects-using-jackson
  62. How to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson: https://stackoverflow.com/questions/11449211/how-to-prevent-null-values-inside-a-map-and-null-fields-inside-a-bean-from-getti
  63. How to tell Jackson to ignore a field during serialization if its value is null?: https://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null
  64. Jackson -- What happens if a property is missing?: https://stackoverflow.com/questions/8320993/jackson-what-happens-if-a-property-is-missing
  65. Jackson -- using @JsonIgnore and @JsonProperty annotations to exclude a property only from JSON deserialization: http://www.davismol.net/2015/03/21/jackson-using-jsonignore-and-jsonproperty-annotations-to-exclude-a-property-only-from-json-deserialization/
  66. How can I tell Jackson to ignore a property for which I don't have control over the source code?: https://stackoverflow.com/questions/7421474/how-can-i-tell-jackson-to-ignore-a-property-for-which-i-dont-have-control-over
  67. No String-argument constructor/factory method to deserialize from String value (): https://stackoverflow.com/questions/45110371/no-string-argument-constructor-factory-method-to-deserialize-from-string-value
  68. Jackson parsing exception -(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value: https://stackoverflow.com/questions/50831335/jackson-parsing-exception-although-at-least-one-creator-exists-no-string-arg
  69. JSON Can not construct instance of JSONConcatination: no String-argument constructor/factory method to deserialize: https://stackoverflow.com/questions/46276449/json-can-not-construct-instance-of-jsonconcatination-no-string-argument-constru
  70. java jackson parse json array: https://stackoverflow.com/questions/49028748/java-jackson-parse-json-array/49052409#49052409
  71. Jackson @JsonProperty and @JsonAlias Example: https://www.concretepage.com/jackson-api/jackson-jsonproperty-and-jsonalias-example (@JsonAlias allows multiple names for a property during JSON to Java Object mapping)
  72. Jackson Mix-in Annotation: https://springframework.guru/jackson-mix-in-annotation/
  73. How do I make jackson not serialize primitives with default value: https://stackoverflow.com/questions/14434679/how-do-i-make-jackson-not-serialize-primitives-with-default-value
  74. Ignore missing properties during Jackson JSON deserialization in Java: https://stackoverflow.com/questions/20578846/ignore-missing-properties-during-jackson-json-deserialization-in-java
  75. How to tell Jackson to ignore a field during serialization if its value is null?: https://stackoverflow.com/questions/11757487/how-to-tell-jackson-to-ignore-a-field-during-serialization-if-its-value-is-null
  76. Jackson @JsonIgnore, @JsonIgnoreProperties and @JsonIgnoreType: https://www.concretepage.com/jackson-api/jackson-jsonignore-jsonignoreproperties-and-jsonignoretype
  77. Do not serialize null values at "mapper class"-level (but always include certain fields even when null) – jackson objectMapper (@JsonInclude): https://makeinjava.com/do-not-serialize-null-values-using-jackson-objectmapper-via-jsoninclude/
  78. Jackson JSON - @JsonInclude NON_EMPTY example: https://www.logicbig.com/tutorials/misc/jackson/json-include-non-empty.html (shows sample outputs of "no annotation" .vs. NON_EMPTY .vs. NON_NULL .vs. NON_ABSENT)
  79. Using Jackson ObjectMapper with Optionals: https://krzysztofgadomski.wordpress.com/2017/05/26/using-jackson-objectmapper-with-optionals/
  80. XSLT equivalent for JSON: http://stackoverflow.com/questions/1618038/xslt-equivalent-for-json
  81. Retreiving JSON From Third Party Servers: https://web.archive.org/web/20140330142238/http://www.hunlock.com/blogs/Mastering_JSON_(_JavaScript_Object_Notation_)#quickIDX11
  82. Java Code Examples for javax.json.JsonObject: https://www.programcreek.com/java-api-examples/?api=javax.json.JsonObject
  83. JSON processing in Java: http://web.archive.org/web/20130814131734/http://docs.oracle.com/javaee/7/tutorial/doc/jsonp001.htm

See Also

REST | XML | AJAX | JavaScript