Search code examples
jsonfield-description

JSON data description language


Assume there are multiple REST APIs that my application wants to access data from. Each API supports JSON responses but differ in the fields used to describe the data. So one API may use time whereas another one may use timestamp as key for the timestamp data. Similarly other fields like location may be represented differently.

One way for me to ingest such heterogeneous data is to write a wrapper for each of the APIs. I, however, want a more generic approach.

My application wants to retrieve (say) the following fields from each API's JSON response:

  • timestamp
  • latitude
  • longitude

I was thinking of writing a single JSON parser but specifying a configuration file which tells the parser which field stands for what. So it would look something like:

For API1 (say)

  • timestamp = 'time'
  • latitude = 'location'[0]
  • longitude = 'location'[1]

For API2 (say)

  • timestamp = 'timestamp'
  • latitude = 'lat'
  • longitude = 'lng'

I am sure there would be existing methods which let you specify such data description, but I have not been able to find one. Could someone point me in the right direction?

EDIT: Please also let me know if there is a generic way of not just parsing but also creating queries for such REST APIs. For example, some APIs accept time interval as query parameter. These query parameters are also different from API to API. So please suggest how to write a generic query module that accepts a config file which tells the module how to form the queries.


Solution

  • I think you are looking for something like JSON Schema.

    From the link:

    A JSON based format for defining the structure of JSON data. JSON Schema provides a contract for what JSON data is required for a given application and how to interact with it. JSON Schema is intended to define validation, documentation, hyperlink navigation, and interaction control of JSON data.

    Here is the example given at the website:

    {
        "title": "Example Schema",
        "type": "object",
        "properties": {
            "firstName": {
                "type": "string"
            },
            "lastName": {
                "type": "string"
            },
            "age": {
                "description": "Age in years",
                "type": "integer",
                "minimum": 0
            }
        },
        "required": ["firstName", "lastName"]
    }