XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
RE: How to represent mixed content in JSON and JSON Schema?

Hi Folks,

 

Thanks to the pointers to web sites showing work with converting XML to JSON. Below is a summary of what I found.

 

Here is a web site with an XSLT program that converts XML to JSON, and it handles mixed content:

 

https://github.com/bramstein/xsltjson/

 

The XSLT program is parameterized, so the XML can be converted to different styles of JSON: the default style, the Badgerfish style, the Rayfish style, and the Rabbitfish style.

 

Let’s see how the different styles convert this XML containing mixed content:

 

<example>Some text <bdo dir="rtl">reverse this text <bdo dir="ltr">not this text</bdo></bdo> finishing text</example>

 

 

Roger’s version

xml-to-json, default output

{
  "example": [
      {"text": "Some text "},
      {"bdo":
          [
              {"dir": "rtl"},
              {"text": "reverse this text "},
              {"bdo":
                  [
                     {"dir": "ltr"},
                     {"text": "not this text"}
                  ]
              }
          ]
      },
      {"text": " finishing text"}
  ]
}

{
    "example": [
        "Some text ",
        {"bdo":
            {"dir":"rtl",
             "$": [
                 "reverse this text ",
                 {"bdo": {

                     "dir":"ltr",
                     "$":"not this text"}
                 }
               ]
           }
       },
       " finishing text"
    ]
}

Summary of this approach: If an XML element has mixed content, then the JSON represents the mixed content using an array of JSON objects – an object for the first item in the mixed content, an object for the second item in the mixed content, etc. If the item in the mixed content is a text node, then the corresponding JSON object has a property “text” with a value that is the value of the text node. The JSON contains an artifact (“text”) that was not in the original XML. Note also that if the element that has mixed content has attributes, then those attribute/value pairs are the first set of objects in the array, e.g., {"dir": "rtl"} is the first object in the array.

Summary of this approach: An XML element with mixed content is represented in JSON either as an array or as a JSON object, depending on whether the element has attributes. If the element has mixed content and no attributes, then the element’s content is represented in JSON with an array. If the element has mixed content and attributes, then the element’s content is represented in JSON with an object: each attribute/value pair is represented as a property/value pair in the JSON object and then the mixed content is represented with a single property/value pair, where the property is “$” and its value is an array. Each text node in the mixed content is represented as a string in the JSON array. Each element in the mixed content is represented as a JSON object.

 

 

 

xml-to-json, default output

xml-to-json, badgerfish output

{
    "example": [
        "Some text ",
        {"bdo":
            {"dir":"rtl",
             "$": [
                 "reverse this text ",
                 {"bdo": {

                     "dir":"ltr",
                     "$":"not this text"}
                 }
               ]
           }
       },
       " finishing text"
    ]
}

{
    "example": [
        {"$":"Some text "},
        {"bdo":
            {"@dir":"rtl",
             "$": [
                 {"$":"reverse this text "},
                 {"bdo": {

                      "@dir":"ltr",
                      "$":"not this text" }
                 }
               ]
            }
        },
        {"$":" finishing text"}
    ]
}

See above.

Summary of this approach: Similar to the default output, but with a couple differences: (1) Attribute names are prefixed with the @ symbol, (2) text nodes in mixed content are represented as JSON objects, where the property name is “$”.  

 

/Roger

 



[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS