Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I express XML tag attributes in JSON?

Tags:

json

xml

People also ask

How are XML attributes represented in JSON?

JSON XML is an SAP-specific representation of JSON data in XML format. The single values, arrays, and objects in JSON are represented as followed in XML. The components of arrays, separated by commas, are mapped as subelements of the element <array>, but the type-specific mapping rule applies to each element.

How XML is converted to JSON?

To convert an XML document to JSON, follow these steps: Select the XML to JSON action from the Tools > JSON Tools menu. Choose or enter the Input URL of the XML document. Choose the path of the Output file that will contain the resulting JSON document.

Does JSON have attributes?

Because JSON does not have attributes, the NIEM attribute nc:personNameInitialIndicator must be treated like a regular property. This requires the addition of a new property to hold the element's original value, so a new name must be provided.

What is JSON and XML write down tag for it?

XML is a markup language, so it has the capacity to display the content. JSON has no tags. XML data is represented in tags, i.e., start tag and end tag. XML file is larger. If we want to represent the data in XML then it would create a larger file as compared to JSON.


Perhaps:

{
  "folders": [
    { "id":123, "private":0, "archived":0, "order":1, "title":"Shopping" },
    ...
  ]
}

Because there is not an exact correspondence between XML and JSON, you are free (e.g. have to define) how the two data-structures map. For instance, in the above, the "folder" element is implicit in the nested objects in the "folders" array.

This could be expanded as in:

"folders": [{"folder": { .... }]

Etc, but there is still the problem of not being able to capture content+attributes as consistently as XML. In any case, your data-structure -> JSON|XML serializer likely works in a particular way (and please, please, use a library, not "hand-rolled" JSON-string-munging). That is; the format of the XML and JSON should be uniformly dictated (somehow) by the data-structure for transmission.


This approach supports inverse transformation to XML:

{
    "folders": {
        "folder":{ 
        "@": {
            "id": "123",
            "private": "0",
            "archived": "0",
            "order": "1"
            },
        "#": "Shopping"
        }
    }
}

It works correctly with js2xmlparser.


There is a JSON notation / convention called badgerfish attempts to standardizes (at least its own terms) the way of preserving of most of the low level XML semantics when XML DOM represented as JSON DOM (with attributes of course) (see http://badgerfish.ning.com/).

So you can easily convert back the badgerfishied-json representation to the XML representation and you still work on the structure with your favorite XML toolset (for me its XPATH / QUERY expressions and tools).

It has also easy to memorize syntax rules (total 9) like: "Attributes go in properties whose names begin with @". You can work on badgerfishied-json in the text editor without overloading your neural circuitry unnecessarily. Usually you can memorize them in the first pass.


An example of how YQL presents XML and the corresponding JSON. No need to know anything about YQL to understand this but if you are interested your can check the YQL console and try it out yourself in the YQL console

XML

<results>
    <a href="/">NBA</a>
    <a class="topnav" href="#">TEAMS</a>
    <a href="/teams/">Teams</a>
    <a href="/hawks/">Atlanta</a>

JSON

"results": {
  "a": [
    {
     "href": "/",
     "content": "NBA"
    },
    {
     "class": "topnav",
     "href": "#",
     "content": "TEAMS"
    },
    {
     "href": "/teams/",
     "content": "Teams"
    },
    {
     "href": "/hawks/",
     "content": "Atlanta"
    },

It seems to me that the most exact correspondence between XML and JSON would need to represent an XML node as a triplet (i.e. array): [name, attributes, value], with name being a string, attributes an object with attribute names as keys and attribute values as (string) values, and value a string (for atomic values) or an array of such triplets.

By such mapping the JSON-equivalent of

<folders>
    <folder id="123" private="0" archived="0" order="1">Shopping</folder>
</folders>

would be

[  "folders",
   {}, 
   [
      [  "folder", 
         {   "id": "123",
             "private": "0",
             "archived": "0",
             "order": "1"
         },
         "Shopping"
      ]
   ]
]

Actually the idea behind this mapping is that:

1) XML-JSON transformation be reversible. 2) The "sibling" relationship of sub-nodes be preserved

At the same time the distinction between attribute nodes and value nodes is explicit here.

Does it make sense? And does it justify the complexity overhead?


Could be compact in the JSON too, attribute is just the same as the value inside tag

from here:

http://www.json.org/example.html

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    },
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    }
}}  

The same text expressed as XML:

<widget>
    <debug>on</debug>
    <window title="Sample Konfabulator Widget">
        <name>main_window</name>
        <width>500</width>
        <height>500</height>
    </window>
    <image src="Images/Sun.png" name="sun1">
        <hOffset>250</hOffset>
        <vOffset>250</vOffset>
        <alignment>center</alignment>
    </image>
</widget>

My choice for XML representation, to make sure it's somehow reversible, is as follows (using your example):

    <folders>
        <folder id="123" private="0" archived="0" order="1">Shopping</folder>
    </folders>

translates to:

    {
      "folders": [
        {
          "folder": [
            {
              "@id": 123,
              "@private": 0,
              "@archived": 0,
              "@order": 1,
              "$t": "Shopping"
            }
          ]
        }
      ]
    }

So by using @ as an indicator for "attribute" and $t as indicator for "text content" I can revert the JSON string to a faithful version of the original XML.

A nice Node package to perform this conversion is XML2JSON, although it doesn't do anything special with the attributes, and some extra code is required to produce this output.