Syntax for documenting JSON structure

南楼画角 提交于 2019-12-02 16:28:37

In theory JSON Schema could serve this purpose, but in practice I am not sure it does. Worth mentioning I hope.

Other than this, my personal opinion is that since JSON is predominantly used for transferring objects, documenting equivalent objects in language client uses (Java, C#, various scripting languages) may make most sense -- after all, such objects usually are mapped/bound to JSON and back. And then you can use whatever documentation tools are available, like Javadoc for Java (perldoc for Perl, Oxygen for c++ etc etc).

For specifying interfaces there is also WADL (Web App Description Language), which might help.

How to generate a HTML Documentation from JSON:

You will need to generate a Json Schema, there is this service that you can paste the orginal JSON and auto generate the Schema:

http://www.jsonschema.net/

With the schema in hands you can auto generate the HTML Documentation using Matic.

https://github.com/mattyod/matic

Generating HTML

To Install Matic you will need install Node.js: http://nodejs.org/

On Windows, run CMD

Install Jade running this command: npm install -g jade

Open the Downloaded Matic folder from Github: cd PATH_TO_FOLDER/matic

Run the install command: npm install -g

Download a documentation example project: https://github.com/mattyod/matic-simple-example

Put your schema in the folder "schemas"

Open the project folder: cd PATH_TO_PROJECT_FOLDER

Run command: matic

You should see a success message: Documentation built to ./web/

SnatchFrigate

I'm unsure to why you're trying to document JSON, I can guess your trying to find a consistent way to tell an IDE or a developer the data types on your notation.

jsdoc (http://jsdoc.sourceforge.net/#usage) might be what your are looking for.

for example:

{
   /**
     * Name of author
     * @type String
     */
   "author": null, 
   /**
     * has the author been clicked
     * @type Boolean
     */
   "clicked": null, 
   /**
     * Unix Timestamp of the creation date
     * @type Int
     */
   "created": null
}

Alternatively if your trying to demonstrate the structure of your data. You could look at YAML (http://www.yaml.org/), it's designed to be a human readable serialisation format which maybe be better suited for documenting your data structure.

A quick example:

Author:
  name: String
  clicked: Boolean
  created: Integer

For simple APIs where each JSON chunk is only one or two levels deep, then documenting by showing examples seems to be the common practice.

However for more complex data models such as yours, I have not seen any good solution. There are some JSON schema proposals, but that seems to go against the spirit of JSON, and seems too heavyweight for your purpose of just documenting.

Personally, I think your scheme is very good. With a few small extensions to handle optional and alternative sections I think it could be just as expressive as Backus-Naur Form, be very easy to read and understand, and be in keeping with the spirit of JSON. Maybe we can get some momentum behind others to use this "Taycher JSON Grammar Form" (TJGF)!

You could write a sample JSON response and then document it using Markdown and Docco. Docco outputs easy to follow HTML based documentation.

It may not be useful in your case since it seems you are not building an API.

But if it was the case and you were using Java or JVM (JAX-RS), you could have used Swagger.

It permits to describes your API in a JSON representation (like WSDL/WADL). And they provide an IHM layer that reads that JSON representation of your API. Here is what you will get: http://petstore.swagger.wordnik.com/

https://developers.helloreverb.com/swagger/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!