Book a Demo

Author Topic: JSON schema model for a list of objects to generate with JSONComposer  (Read 5916 times)

heba

  • EA User
  • **
  • Posts: 44
  • Karma: +0/-0
    • View Profile
Hi,

does anybody know how an attribute of a JSON schema element has to look like to get a definition for a list of objects?

What I want is a schema model for something like this:
{
  "applications": {
    "app_1": {
      "path": "/home/my",
      "permission": 100
    },
    "app_2" : 
          ... and so on
  }
}


The related part of the generated schema looks as follows:

  "properties": {
    "applications": {
      "description": "",
      "type": "array",
      "items": {
        "$ref": "#/definitions/Application"
      },
      "uniqueItems": true
    }
  }


Here, type "array" is the problem. If I change it to "object" the JSON file validates successfully.

Sorry, don't know how to attach pictures or a model, so --> https://imgur.com/eks3Qkg

Thank you
Heiko

pvickers

  • EA User
  • **
  • Posts: 44
  • Karma: +7/-0
    • View Profile
Re: JSON schema model for a list of objects to generate with JSONComposer
« Reply #1 on: January 11, 2020, 04:08:48 am »
Hi Heiko,

The result you get from the Schema Composer is largely determined by the underlying class/attribute definitions.

An attribute will be defined as an "array" if you have set the "Is Collection" property to "true". (See the attribute's properties under "Multiplicity"...)

Other JSON syntax is determined by how you have defined the schema, for example the root node, what classes/attributes are included... while working with the Schema Composer definition.

Define your classes as required, including contained structures, then add that class to your schema.

I hope this helps.

Regards,
Perry

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: JSON schema model for a list of objects to generate with JSONComposer
« Reply #2 on: January 13, 2020, 08:48:32 am »
The problem is that your example document does contain an object, not an array. I don't know enough about json schemas if there is an equivalent to xsd:any, but that's what you would need to implement "and so on" in an object.

If you actually want an array it would look like this:
Code: [Select]
{
  "applications": [
    {
      "path": "/home/my",
      "permission": 100
    },
    {
      ...
    },
          ... and so on
  ]
}


heba

  • EA User
  • **
  • Posts: 44
  • Karma: +0/-0
    • View Profile
Re: JSON schema model for a list of objects to generate with JSONComposer
« Reply #3 on: January 13, 2020, 09:03:33 pm »
Thank you for the answers.

The "Is Collection" property does not seem to make a difference. As soon as I change the multiplicity to something else than 1..1 it get an array.  However, I learned that changing "array" to "object" only works because "items" does not seem to apply afterwards which makes the validation successful.  ::)

The reason why I do not want an array is that I use JSON pointers to access the data element, for example "/applications/app_2/permission". It is getting even worse because this identifier can also contain more subelements (like directory structure), e.g. "/applications/group_1/subgroup_1/app_1/..", and I was no able to put that into a clean class model.

I will now try to put this into a well-formed class structure first.

Heiko

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: JSON schema model for a list of objects to generate with JSONComposer
« Reply #4 on: January 15, 2020, 09:25:54 am »
JSON pointers do support arrays from my quick search.

/applications/2/permission
/applications/1/1/

https://rapidjson.org/md_doc_pointer.html