Recursive entity representation in JSON schema.

1,840 views
Skip to first unread message

mitchell....@gmail.com

unread,
Mar 28, 2011, 11:05:50 AM3/28/11
to JSON Schema
Hi Everyone,

I have recently been doing some reading about JSON schema because I
am very interested in utilizing it rather than XML (mostly due to my
desire to work with very specific open source software). My
question is focused around statements that a JSON schema can NOT
facilitate a recursive structure. This is a problem for my data
representation because I need to convert the following scenario from
XML to JSON.. (this is a sample XML, not my actual data-set)

<tns:Folder name="Library">
<tns:Folder name="Documents">
<tns:Folder name="CIS2250">
<tns:File name="TechManual.pdf" />
<tns:File name="UserManual.pdf" />
</tns:Folder>
<tns:File name="MyFile1.txt" />
<tns:File name="MyFile2.txt" />
<tns:File name="MyFile3.txt" />
</tns:Folder>
<tns:Folder name="Downloads">
<tns:File name="EatMyShorts.mpeg" />
<tns:File name="BackToTheFuture-Part1.avi" />
<tns:File name="" />
</tns:Folder>
</tns:Folder>

Notice that the tns:Folder XML entity can be recursively represented
in a XML schema. After the XML Schema definition I utilize a tool
called Jibx to automatically create clone Java classes, and then
populate those classes with instances of a XML instance during
execution of my program.

Overall my question is... can an entity within a JSON schema hold a
collection of a it's own entity classification? Some online
publications have suggested to utilize YAML instead of JSON, but for
my purposes the decision lies between XML or JSON.

All the best,
Mitch

Kenny Hoxworth

unread,
Mar 28, 2011, 12:54:03 PM3/28/11
to json-...@googlegroups.com
JSON Schema can most definitely handle recursive structures as of draft-03. Draft-03 introduced the "$ref" attribute, which allows a schema to reference another schema (or itself) to validate against. In the case of a filesystem hierarchy, assume a JSON representation of the data as follows:

{
  "type" : "folder",
  "name" : "documents",
  "contents" : [
    {
      "type" : "folder",
      "name" : "testDir",
      "contents" : []
    },
    {
      "type" : "file",
      "name" : "test.txt"
    }
  ]
}

As JSON schema that represents this data would be as follows:

{
  "type" : "object",
  "id" : "folder",
  "additionalProperties" : false,
  "properties" : {
    "name" : { "required" : true, "type" : "string" },
    "type" : { "required" : true, "type" : "string", "enum" : ["folder"] },
    "contents" : {
      "required" : true,
      "type" : "array",
      "additionalItems" : false,
      "items" : [
        { "$ref" : "folder" },
        {
          "type" : "object",
          "id" : "file",
          "additionalProperties" : false,
          "properties" : {
            "name" :  { "required" : true, "type" : "string" },
            "type" : { "required" : true, "type" : "string", "enum" : ["file"] }
          }
        }
      ]
    }
  }
}


--
You received this message because you are subscribed to the Google Groups "JSON Schema" group.
To post to this group, send email to json-...@googlegroups.com.
To unsubscribe from this group, send email to json-schema...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/json-schema?hl=en.


yott...@gmail.com

unread,
Nov 6, 2015, 9:59:26 PM11/6/15
to JSON Schema, mitchell....@gmail.com
Does anyone know a tool that will generate Java from a recursive JSON instance without duplicating classes, or will generate tight JSON schema from recursive JSON instances?

Thanks,

John Carlson
Reply all
Reply to author
Forward
0 new messages