JSON2ASN — Help

This web app allows you to generate an ASN.1 schema for/from your JSON. With an ASN.1 schema you can serialize and validate your messages in a variety of formats - binary (DER, PER, OER, etc) and text (XML and JSON).

JSON2ASN is a web tool that allows you to create a ASN.1 schema from either a JSON message or a JSON Schema. Using the generated ASN.1 schema, you can then automatically generate code to serialize and parse your data in a variety of formats, and validate any message for conformance with the schema.

These are the scenarios in which this tool may be helpful.

  • When your protocol matures, you will need a schema for it. This tool allows a quick way to develop a schema using your sample messages.
  • You already have a JSON Schema, but you want to migrate to ASN.1.
  • If you know JSON better than you know ASN.1, you may describe your data in JSON, then convert this JSON into ASN.1 schema.

ASN.1 is a proven standard that's been developed since 1980s, and has been already used in serializing and parsing many types of messages, from the simplest to the most complex.

While ASN.1 offers many features, this web tool uses a small subset of them to simplify writing and understanding of the schemas. Below you can see an example of a JSON message with its generated ASN.1 schema.

{
 "name":"Falcon",
 "message":"Hello World",
 "fuel":"liquid",
 "speed":{
  "mph":18000
 },
 "payload":["Car", "GPS unit"]
}

SEQUENCE {
 name UTF8String,
 message UTF8String,
 fuel UTF8String,
 speed SEQUENCE {
  mph INTEGER
 },
 payload SEQUENCE OF UTF8String
}

Once you generate your ASN.1 schema, you can:

  • edit it to add new fields, remove unneeded fields or modify existing ones.
  • use any ASN.1 compiler to generate the model and parsing/serializing code, and use a variety of formats to create and read messages, either text-based (JSON) or binary (DER, OER, PER, etc)
  • use a tool (e.g. Dynamic Codec) to directly validate JSON messages against your schema
JSON message: lists
[
 {"id":1, "name":"File"},
 {"id":2, "name":"Edit"},
 { "name":""}
]

SEQUENCE OF SEQUENCE {
 name UTF8String,
 id INTEGER OPTIONAL
}


JSON message: a collection of related objects
{
"shapes" :  [
  {"area":3.14,"radius":1},
  {"area":1,"x":1,"y":1},
  {"area":2,"b":1,"h":2},
 ]
}


shapes SEQUENCE OF SEQUENCE {
 area INTEGER,
 y INTEGER OPTIONAL,
 x INTEGER OPTIONAL,
 radius INTEGER OPTIONAL,
 h INTEGER OPTIONAL,
 b INTEGER OPTIONAL
}

JSON message: tuples
{
"location" :  [
  “New York”,
  8550405,
  790,
  {"x":-74.96778,"y":49.6743}
 ]
}

location [ARRAY] SEQUENCE {
 item0 UTF8String,
 item1 INTEGER,
 item2 INTEGER,
 item3 SEQUENCE {
  y REAL,
  x REAL }
}

JSON message: an unordered collection of various objects
{
"everything" :  [
  {
   "model":"Toyota",
   "passengers":5,
   "auto":true
  },
  {
   "brand":"Absolut",
   "proof":80
  },
  {
   "material":"wood",
   "country":"USA",
   "year":2017
  }
 ]
}

everything SEQUENCE OF [JER: UNWRAPPED] CHOICE {
 alternative0 SEQUENCE {
  passengers INTEGER,
  model UTF8String,
  auto BOOLEAN
 },
 alternative1 SEQUENCE {
  proof INTEGER,
  brand UTF8String
 },
 alternative2 SEQUENCE {
  year INTEGER,
  material UTF8String,
  country UTF8String
 }
}



JSON Schema: oneOf
{
 "oneOf": [
  { "type": "string", "maxLength": 5 },
  { "type": "number", "minimum": 0 }
 ]
}

CHOICE {
 alt1 UTF8String(SIZE(MIN..5)),
 alt2 REAL(0.0..MAX)
}



You can include in the JSON message several array elements with either all the fields or with some fields missing. If you select the "List of similar items" option in the Arrays section, the JSON2ASN tool will try to find the minimum schema that will fit all the elements using OPTIONAL fields, as can be seen in the example below:

[
 {
  "name":"Adam"
 },
 {
  "name":"Adam",
  "first-words":"Hello World"
 },
 {
  "age":{
   "biblical":930
  }
 }
]

SEQUENCE OF SEQUENCE {
 age SEQUENCE {
  biblical INTEGER
 } OPTIONAL,
 name UTF8String OPTIONAL,
 first-words UTF8String OPTIONAL
}








If you have an array of different elements, you can choose the "An unordered collection of various items" option in the Arrays section to generate a schema that allows for such elements, as you can see in the example below:

{
"everything": {
 [
  {
   "model":"Toyota",
   "passengers":5,
   "auto":true
  },
  {
   "brand":"Absolut",
   "proof":80
  },
  {
   "material":"wood",
   "country":"USA",
   "year":2017
  }
 ]
}

everything SEQUENCE OF [JER: UNWRAPPED] CHOICE {
 alternative0 SEQUENCE {
   model UTF8String,
   passengers INTEGER,
   auto BOOLEAN
 },
 alternative1 SEQUENCE {
   brand UTF8String,
   proof INTEGER
 },
 alternative2 SEQUENCE {
   material UTF8String,
   country UTF8String,
   year
 }
}




*Note: to generate an exact schema, Encoding Instructions are needed. To obtain this, please also check the "Allow ASN.1 Encoding Instructions" option in the Other section.
For more information on Encoding Instructions, please read the section regarding Encoding Instructions below.

In order for your schema to support adding new fields in future versions of it, the JSON2ASN offer options like "Make objects extensible" or "additionalProperties":true (a JSON Schema property). The generated schema will contain the extensible marker ("...") for each SEQUENCE and CHOICE, which will allow to correctly decode older messages.
Also consider marking schema fields as OPTIONAL, so they can be deprecated with new schema versions.

JSON message with "Make objects extensible"
{
 "name":"Falcon"
}


SEQUENCE {
 name UTF8String,
 ...
}

JSON Schema with "additionalProperties":true
{
 "type": "object",
 "properties": {
  "carMake": { "type": "string" },
  "carModel": { "type": "string" }
 },
 "additionalProperties": true
}

SEQUENCE {
 carMake UTF8String OPTIONAL,
 carModel UTF8String OPTIONAL,
  ...
}




ASN.1 schemas not only define data structures, but may also govern data encoding (serialization) details. Encoding Instructions specifying such details.

For more information on Encoding Instructions, please read https://www.oss.com/asn1/resources/asn1-papers/Overview%20of%20JER.pdf

  • To apply various schema generation options to various parts of a message split it into multiple objects and get several schemas, then re-combine schemas into one.
  • Combine multiple JSON messages into an array to get a single ASN.1 schema across them all (e.g. [{"message":..... }, {"message":.....}, ........]).
  • Provide default values, it will allow more compact encodings.
  • In an array do not use null to skip objects, use {} instead. ASN.1 arrays (SEQUENCE OF) have no notion of a null element.
  • In a JSON Schema consider using enum with a single element instead of const