When is using ASN.1 preferable to using JSON? What are some advantages and disadvantages of both approaches?
3 Answers
ASN.1 and JSON aren't strictly comparable. JSON is a data format. ASN.1 is a schema language plus multiple sets of encoding rules, each of which produces different data formats for a given schema. So, the original question somewhat parallels the question "XML Schema vs. XML: when is it appropriate to use them?" A fairer comparison would be between ASN.1 and JSON Schema.
That said, a few points to consider:
- ASN.1 has binary encoding rules. Consider whether binary or text encoding is preferable for your application.
- ASN.1 also has XML and JSON encoding rules. You can opt to go with a text-based encoding using ASN.1, if you like.
- ASN.1 allows other encoding rules to be developed. Before ITU-T specified encoding rules for JSON, we specified our own rules to encode ASN.1 to JSON. I blogged about this on our company website here
- As with XML Schema, tools exist for compiling ASN.1. These are commonly referred to as data binding tools. The compiler output consists of data structures to hold your data, and code for encoding/decoding to/from the various encodings (binary, XML, JSON).
- I am not sure what, if any, data binding tools exist for JSON Schema. I am also not sure how mature/stable JSON Schema is, whereas ASN.1 is quite mature and stable.
- Choosing between JSON Schema and ASN.1, note that JSON Schema is bound to JSON, whereas ASN.1 is not bound to any particular representation.

- 1,876
- 12
- 17
-
One thing I have seen in JSON tools / libraries is that in some languages they give up. I gather that you can express a union in a JSON schema, but I've heard that C# tools give up on that because C# doesn't have unions. Whereas all the ASN.1 tools I've used in C# have classes that do very well (which CHOICEs). I think GPB (which has oneof) does OK in C# too. – bazza Jan 13 '21 at 21:07
You can use ASN.1 regardless of whether you need to serialize messages that might go to a recipient using C, C++, C#, Java, or any other programming language with ASN.1 encoder/decoder engine. ASN.1 also provides multiple encoding rules which have benefits under different circumstances. For example, DER is used when a canonical encoding is crucial, such as in digital certificates, while PER is used when bandwidth is critical such as in cellular protocols, and E-XER is used when you don't care about bandwidth and would like to display an encoding in XML for maniplulation in a browser or exchange messages with an XML Schema engine.
Note that with a good ASN.1 tool, you don't have to change you application code to switch between these ASN.1 encoding rules. A simple function call can select the encoding rules you would like to use.

- 1,930
- 17
- 23
-
2
-
My understanding is that by JSON, you meant JavaScript Object Notation. Are you telling me this has nothing to do with Java even though Java is part of its name? – Paul Thorpe Jul 03 '12 at 20:54
-
2Ok. I did not know as much about the text format JSON, but I do know ASN.1 quite well, and it has the flexibility to produce either very compact binary encodings (PER) or verbose XML encodings depending on what your needs are. – Paul Thorpe Jul 05 '12 at 21:03
-
I edited the answer to remove the part about not being tied to Java. – Paul Thorpe Jul 05 '12 at 21:24