contentEncoding : String

contentEncoding

String

The string instance should be interpreted as encoded binary data and decoded using the encoding named by this property.

Value This keyword should be set to a standard (to increase interoperability) encoding name such as those defined in RFC 4648
Kind Annotation
Applies To String
Dialect 2020-12
Changed In None
Introduced In Draft 7
Vocabulary Content
Specification https://json-schema.org/draft/2020-12/json-schema-validation.html#section-8.3
Metaschema https://json-schema.org/draft/2020-12/meta/content
Official Tests draft2020-12/content.json
Default None
Annotation String The content encoding name set by this keyword
Affected By None
Affects None
Also See

The contentEncoding keyword is an annotation used to specify the encoding used to store the contents of a string, particularly when it represents binary data. It indicates how the string value should be interpreted and decoded. This keyword is not directly involved in the validation process but provides metadata about the content.

  • contentEncoding doesn’t enforce strict validation. However, it’s recommended to use it correctly to ensure compatibility with applications that might interpret the encoding.
  • It represents the type of binary encoding used for the string under question. Some of the common encodings are listed here.
  • The JSON Schema specification doesn’t publish a predefined list of possible encodings

Common Encodings

Encoding Description Reference
"base16" Encoding scheme for binary data using a 16-character hexadecimal alphabet RFC 4648 §8
"base32" Encoding scheme for binary data using a 32-character hexadecimal alphabet RFC 4648 §6
"base64" Encoding scheme for binary data using a 64-character hexadecimal alphabet RFC 4648 §4

Examples

Schema with 'contentEncoding' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "contentEncoding": "base64"
}
Valid A properly encoded base64 string is valid Instance
"SGVsbG8gV29ybGQ="    // --> Hello World (base64 encoded)
Valid An incorrectly encoded base64 string is also valid Instance
"This is not base64 encoded!"
Valid 'contentEncoding' is irrelevant for instances with values other than strings Instance
[ "foo", "bar" ]
Annotations
{ "keyword": "/contentEncoding", "instance": "", "value": "base64" }