Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

javascript - json-schema: how to transform from one json-schema to another

I have the two different json-schemas:

schemaA -> A calendar as defined at http://json-schema.org/calendar

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description": "A representation of an event",
    "type": "object",
    "required": [ "dtstart", "summary" ],
    "properties": {
        "dtstart": {
            "format": "date-time",
            "type": "string",
            "description": "Event starting time"
        },
        "dtend": {
            "format": "date-time",
            "type": "string",
            "description": "Event ending time"
        },
        "summary": { "type": "string" },
        "location": { "type": "string" },
        "url": { "type": "string", "format": "uri" },
        "duration": {
            "format": "time",
            "type": "string",
            "description": "Event duration"
        },
        "rdate": {
            "format": "date-time",
            "type": "string",
            "description": "Recurrence date"
        },
        "rrule": {
            "type": "string",
            "description": "Recurrence rule"
        },
        "category": { "type": "string" },
        "description": { "type": "string" },
        "geo": { "$ref": "http: //json-schema.org/geo" }
    }
}

schemaB -> Another calendar schema (also json-schema version draft-04)

My quesiton is simple. I have a javascript object 'calendarA' that follows the first schema, i.e.,

validates(calendarA, schemaA); // true

I want to describe a transformation between the schemas, i.e., from schemaA to schemaB, so I can pass calendarA as input and get a new calendarB that fits schemaB. Put it in code:

var calendarB = fromSchemaAtoB(calendarA, schemaA, schemaB);
validates(calendarB, schemaB); // true

From your point of view which is the best approach/tools to write fromSchemaAtoB guys? I really want to describe transfomations using the schemas, something like:

schemaB.properties.foo = schemaA.properties.dtstart

I saw a lot of basic json transformation packages but it seems to me that in most of them you specify your output as external templates that do not take into account the schemas (so result could be invalid with respect to schemaB).

Thank you so much in advance!!

JG

PS: I prefer javascript based solutions if possible but I am really open to any possibility.

EDIT 1: To clarify after reading @jason's answer, the question is how to better describe such relations between schemas and how to apply them to obtain calendarB. So if you prefer:

var calendarB = transform(calendarA, schemaA, schemaB, relationsAtoB);
validates(calendarB, schemaB); // true

and the question then is how to better describe "relationsAtoB" and how to implement the "transform" function.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I believe a library like this could be used to address your question. This does not directly address the question (transforming from one JSON schema to the other) but what you can do (which is what I am currently doing) is the following:

  1. Specify a JSON schema for your input
  2. Specify a JSON schema for your output
  3. Specify a mapping template (e.g. using the library I referenced).

Of course, ideally you would not have to do both 2 and 3 but I have not found something which does this automatically. So, for example, you could specify the mapping template and create some library function which takes that as well as the JSON schema in 1 as its inputs, and would generate the JSON schema in 3 as its output.

This, however, is not trivial so currently I am specifying both 2 and 3.

Also, keep in mind that you cannot have 1 and 2 and somehow automatically generate 3. This is because there are more than one mapping functions that would take data adhering to schema 1 and produce data adhering to schema 2.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...