JavaScript formatting json value depending schema

59 views Asked by At

Is there a simple way to vor a value of JSON field depending on format on its JSON Schema (specially date / datetime format for mongodb)? Examples: JSON:

{
  "firstName": "Alex",
  "lastName": "Alex",
  "birthDate": "1996-06-20"
}

Schema:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {},
    "id": "http://example.com/example.json",
    "properties": {
        "birthDate": {
            "id": "/properties/birthDate",
            "type": "string",
              "format":"date"
        },
        "firstName": {
            "id": "/properties/firstName",
            "type": "string"
        },
        "lastName": {
            "id": "/properties/lastName",
            "type": "string"
        }
    },
    "type": "object"
}

Final result after conversion:

{
  "firstName": "Alex",
  "lastName": "Alex",
  "birthDate": {
    $date": 835228800000
  }
}

(I am using this format because I am using RestHeart)

And in case of datetime do same thing. This formating must be generic and valid for nested objects also.

Thanks for your help

1

There are 1 answers

0
Nader Sghir On BEST ANSWER

I developed a Node.js module to resolve this problem: Here is the link for whom interested: https://www.npmjs.com/package/mongodates