Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are JSON and XML Comparable? [closed]

I heard someone state that "JSON is the same as XML." Are JSON and XML comparable? What are the major similarities and differences of each?

There are a few StackOverflow Q&As which touch on comparing JSON and XML [1] [2] [3] [4] [5] [6] [7], but none provide a good, single point of reference focused on their major similarities and differences.

like image 855
Dan Cruz Avatar asked Oct 19 '11 15:10

Dan Cruz


People also ask

Is XML equivalent to JSON?

XML is a document markup language, providing semantics that gives additional meaning to data; JSON does not have this ability. XML has a broader range of specifications that cover such things as schemas for data definition and validation, namespaces, stylesheets, transformations, data expressions and many others.

How does JSON compare to XML?

JSON object has a type whereas XML data is typeless. JSON does not provide namespace support while XML provides namespaces support. JSON has no display capabilities whereas XML offers the capability to display data. JSON is less secured whereas XML is more secure compared to JSON.


2 Answers

What is JSON?

JSON (JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers.

Borrowing a JSON sample from Wikipedia, a JSON representation of a person might look like:

{
    "firstName" : "John",
    "lastName"  : "Smith",
    "address"   :
    {
        "street": "21 2nd Street",
        "city"  : "New York",
        "state" : "NY",
        "zip"   : "10021"
    },
    "phoneNumber":
    [
        {
            "type"  : "home",
            "number": "212 555-1234"
        },
        {
            "type"  : "fax",
            "number": "646 555-4567"
        }
    ]
}

What is XML?

XML (eXtensible Markup Language) is a simple, very flexible text format derived from SGML. According to Wikipedia, XML is a set of rules for encoding documents in machine-readable form. [... It's goals] emphasize simplicity, generality, and usability over the Internet. It is a textual data format[...].

Again borrowing a XML sample from Wikipedia, an XML representation of a person might look like:

<person firstName="John" lastName="Smith">
    <address street="21 2nd Street" city="New York" state="NY" zip="10021" />
    <phoneNumber type="home" number="212 555-1234" />
    <phoneNumber type="fax"  number="646 555-4567" />
</person>

Comparison

Based on these simple definitions, it's understandable that one could conclude that JSON and XML are comparable. If your goal is to use one or the other for simple data interchange over the Internet, as exhibited by the simple examples above, that goal is surely attainable and JSON and XML are, indeed, mostly comparable.

However, as you dig deeper into the specifications of each you begin to realize they are completely different standards with similar goals; each with overlapping use cases where one is much better suited than the other and where they are equivalent solutions (where the choice is a matter of the specific use case).

Though it may be just a bit biased, The Fat-Free Alternative to XML from the JSON site lists some good points of comparison. There are also the XML vs JSON !!! Dont compare. and JSON Pros and Cons blog entries with some bullet points for comparison.

Similarities

  • May be used for textual data interchange; no obscure format that is not easily readable by humans.
  • Allows data to be represented in a structured manner, providing a level of data relationships.
  • Internationalization using Unicode.
  • Programming language agnostic; may be used in many programming languages.
  • May be used to interface heterogeneous systems; as long as all systems use an agreed data representation contract, communication between heterogeneous systems is possible.
  • Open standards; membership to any governing body is not required to acquire the specifications (JSON is defined in the IETF RFC #4627 and XML is defined as a W3C specification) and there are no restrictive licenses (JSON, XML).

Differences

  • JSON has a simple notation that may be quickly adopted by developers.
  • JSON's lightweight nature lends it towards improved Internet user experience by decreasing performance bottlenecks.
  • JSON's simple notation, borrowed from JavaScript, makes it easier and more performant to de/serialize the data representation to common data structures.
  • JSON is available in ECMAScript, 5th edition, making it available to all applications (most notably, web browsers) with an integrated ECMAScript engine.
  • XML is a document markup language, providing semantics that gives additional meaning to data; JSON does not have this ability.
  • XML has a broader range of specifications that cover such things as schemas for data definition and validation, namespaces, stylesheets, transformations, data expressions and many others.
  • XML has been around longer and is widely adopted by many businesses; affording it greater documentation, programming language support, tooling support, community experience, off-the-shelf product support, etc.
  • XML's robust standards makes it a better solution for flexible (or sometimes, rigid ... in a beneficial way) business-to-business communication.
like image 127
Dan Cruz Avatar answered Sep 22 '22 05:09

Dan Cruz


Here is a set of analogous relationships which can be used to convert from XML to JSON:

  • The root node is represented by the top-level Object
  • The set of childnodes plus attributes is represented by an Array
  • Each childnode is represented by a String name with an Object value
  • Each attribute is represented by a String name with a String value
  • Elements that may appear more than once are converted to Array properties
  • Text values of tags are represented by a prefixed String name with a String value
  • Comment nodes are merged with the nearest String value plus \u0022 delimiters

Here is a set of analogous relationships which can be used to convert from JSON to XML:

  • The top-level Object is represented by the root node
  • The Array is represented by the set of childnodes in the childnodes axis
  • The String name with an Object value is represented by the element entity
  • The String name with a String value is represented by the attribute entity
  • String names which have prefixes are converted to text nodes of the parent element

References

  • JXON Library: JavaScript XML Object Notation
  • JSON-Schema
  • RDF Schemas JSON serialization
  • XML or JSON: Guidelines for what to choose for DB2 for z/OS
  • Build a pureXML and JSON application, Part 1: Store and query JSON with DB2 pureXML
  • Build a pureXML and JSON application, Part 2: Create Universal Services for pureXML that expose JSON
  • Build a pureXML and JSON application, Part 3: Create OpenSocial gadgets for pureXML
  • GData: About JSON Feeds and XML Feeds
  • YQL: XML to JSON
  • YQL: JSONP-X, JSON envelope with XML content
  • Microdata Demo: JSON to N3 to N-Triples
  • RDF vs OData vs GData
  • RDFa API
  • JSON-LD API
  • Horses for courses: A perspective on an XML vs. JSON discussion
like image 27
9 revs Avatar answered Sep 23 '22 05:09

9 revs