JSON-Cadence Data Interchange Format

7 min read

Version 0.3.0

JSON-Cadence is a data interchange format used to represent Cadence values as language-independent JSON objects.

This format includes less type information than a complete ABI, and instead promotes the following tenets:

  • Human-readability - JSON-Cadence is easy to read and comprehend, which speeds up development and debugging.
  • Compatibility - JSON is a common format with built-in support in most high-level programming languages, making it easy to parse on a variety of platforms.
  • Portability - JSON-Cadence is self-describing and thus can be transported and decoded without accompanying type definitions (i.e. an ABI).

Values


Void

1
{
2
"type": "Void"
3
}

Example

1
{
2
"type": "Void"
3
}

Optional

1
{
2
"type": "Optional",
3
"value": null | <value>
4
}

Example

1
// Non-nil
2
3
{
4
"type": "Optional",
5
"value": {
6
"type": "UInt8",
7
"value": "123"
8
}
9
}
10
11
// Nil
12
13
{
14
"type": "Optional",
15
"value": null
16
}

Bool

1
{
2
"type": "Bool",
3
"value": true | false
4
}

Example

1
{
2
"type": "Bool",
3
"value": true
4
}

String

1
{
2
"type": "String",
3
"value": "..."
4
}

Example

1
{
2
"type": "String",
3
"value": "Hello, world!"
4
}

Address

1
{
2
"type": "Address",
3
"value": "0x0" // as hex-encoded string with 0x prefix
4
}

Example

1
{
2
"type": "Address",
3
"value": "0x1234"
4
}

Integers

[U]Int, [U]Int8, [U]Int16, [U]Int32,[U]Int64,[U]Int128, [U]Int256, Word8, Word16, Word32, or Word64

Although JSON supports integer literals up to 64 bits, all integer types are encoded as strings for consistency.

While the static type is not strictly required for decoding, it is provided to inform client of potential range.

1
{
2
"type": "<type>",
3
"value": "<decimal string representation of integer>"
4
}

Example

1
{
2
"type": "UInt8",
3
"value": "123"
4
}

Fixed Point Numbers

[U]Fix64

Although fixed point numbers are implemented as integers, JSON-Cadence uses a decimal string representation for readability.

1
{
2
"type": "[U]Fix64",
3
"value": "<integer>.<fractional>"
4
}

Example

1
{
2
"type": "Fix64",
3
"value": "12.3"
4
}

Array

1
{
2
"type": "Array",
3
"value": [
4
<value at index 0>,
5
<value at index 1>
6
// ...
7
]
8
}

Example

1
{
2
"type": "Array",
3
"value": [
4
{
5
"type": "Int16",
6
"value": "123"
7
},
8
{
9
"type": "String",
10
"value": "test"
11
},
12
{
13
"type": "Bool",
14
"value": true
15
}
16
]
17
}

Dictionary

Dictionaries are encoded as a list of key-value pairs to preserve the deterministic ordering implemented by Cadence.

1
{
2
"type": "Dictionary",
3
"value": [
4
{
5
"key": "<key>",
6
"value": <value>
7
},
8
...
9
]
10
}

Example

1
{
2
"type": "Dictionary",
3
"value": [
4
{
5
"key": {
6
"type": "UInt8",
7
"value": "123"
8
},
9
"value": {
10
"type": "String",
11
"value": "test"
12
}
13
}
14
],
15
// ...
16
}

Composites (Struct, Resource, Event, Contract, Enum)

Composite fields are encoded as a list of name-value pairs in the order in which they appear in the composite type declaration.

1
{
2
"type": "Struct" | "Resource" | "Event" | "Contract" | "Enum",
3
"value": {
4
"id": "<fully qualified type identifier>",
5
"fields": [
6
{
7
"name": "<field name>",
8
"value": <field value>
9
},
10
// ...
11
]
12
}
13
}

Example

1
{
2
"type": "Resource",
3
"value": {
4
"id": "0x3.GreatContract.GreatNFT",
5
"fields": [
6
{
7
"name": "power",
8
"value": {"type": "Int", "value": "1"}
9
}
10
]
11
}
12
}

Path

1
{
2
"type": "Path",
3
"value": {
4
"domain": "storage" | "private" | "public",
5
"identifier": "..."
6
}
7
}

Example

1
{
2
"type": "Path",
3
"value": {
4
"domain": "storage",
5
"identifier": "flowTokenVault"
6
}
7
}

Type Value

1
{
2
"type": "Type",
3
"value": {
4
"staticType": <type>
5
}
6
}

Example

1
{
2
"type": "Type",
3
"value": {
4
"staticType": {
5
"kind": "Int",
6
}
7
}
8
}

Capability

1
{
2
"type": "Capability",
3
"value": {
4
"path": <path>,
5
"address": "0x0", // as hex-encoded string with 0x prefix
6
"borrowType": <type>,
7
}
8
}

Example

1
{
2
"type": "Capability",
3
"value": {
4
"path": "/public/someInteger",
5
"address": "0x1",
6
"borrowType": {
7
"kind": "Int"
8
},
9
}
10
}

Types

Simple Types

These are basic types like Int, String, or StoragePath.

1
{
2
"kind": "Any" | "AnyStruct" | "AnyResource" | "Type" |
3
"Void" | "Never" | "Bool" | "String" | "Character" |
4
"Bytes" | "Address" | "Number" | "SignedNumber" |
5
"Integer" | "SignedInteger" | "FixedPoint" |
6
"SignedFixedPoint" | "Int" | "Int8" | "Int16" |
7
"Int32" | "Int64" | "Int128" | "Int256" | "UInt" |
8
"UInt8" | "UInt16" | "UInt32" | "UInt64" | "UInt128" |
9
"UInt256" | "Word8" | "Word16" | "Word32" | "Word64" |
10
"Fix64" | "UFix64" | "Path" | "CapabilityPath" | "StoragePath" |
11
"PublicPath" | "PrivatePath" | "AuthAccount" | "PublicAccount" |
12
"AuthAccount.Keys" | "PublicAccount.Keys" | "AuthAccount.Contracts" |
13
"PublicAccount.Contracts" | "DeployedContract" | "AccountKey" | "Block"
14
}

Example

1
{
2
"kind": "UInt8"
3
}

Optional Types

1
{
2
"kind": "Optional",
3
"type": <type>
4
}

Example

1
{
2
"kind": "Optional",
3
"type": {
4
"kind": "String"
5
}
6
}

Variable Sized Array Types

1
{
2
"kind": "VariableSizedArray",
3
"type": <type>
4
}

Example

1
{
2
"kind": "VariableSizedArray",
3
"type": {
4
"kind": "String"
5
}
6
}

Constant Sized Array Types

1
{
2
"kind": "ConstantSizedArray",
3
"type": <type>,
4
"size": <length of array>,
5
}

Example

1
{
2
"kind": "ConstantSizedArray",
3
"type": {
4
"kind": "String"
5
},
6
"size":3
7
}

Dictionary Types

1
{
2
"kind": "Dictionary",
3
"key": <type>,
4
"value": <type>
5
}

Example

1
{
2
"kind": "Dictionary",
3
"key": {
4
"kind": "String"
5
},
6
"value": {
7
"kind": "UInt16"
8
},
9
}

Composite Types

1
{
2
"kind": "Struct" | "Resource" | "Event" | "Contract" | "StructInterface" | "ResourceInterface" | "ContractInterface",
3
"type": "", // this field exists only to keep parity with the enum structure below; the value must be the empty string
4
"typeID": "<fully qualified type ID>",
5
"initializers": [
6
<initializer at index 0>,
7
<initializer at index 1>
8
// ...
9
],
10
"fields": [
11
<field at index 0>,
12
<field at index 1>
13
// ...
14
],
15
}

Example

1
{
2
"kind": "Resource",
3
"type": "",
4
"typeID": "0x3.GreatContract.GreatNFT",
5
"initializers":[
6
[
7
{
8
"label": "foo",
9
"id": "bar",
10
"type": {
11
"kind": "String"
12
}
13
}
14
]
15
],
16
"fields": [
17
{
18
"id": "foo",
19
"type": {
20
"kind": "String"
21
}
22
}
23
]
24
}

Field Types

1
{
2
"id": "<name of field>",
3
"type": <type>
4
}

Example

1
{
2
"id": "foo",
3
"type": {
4
"kind": "String"
5
}
6
}

Parameter Types

1
{
2
"label": "<label>",
3
"id": "<identifier>",
4
"type": <type>
5
}

Example

1
{
2
"label": "foo",
3
"id": "bar",
4
"type": {
5
"kind": "String"
6
}
7
}

Initializer Types

Initializer types are encoded a list of parameters to the initializer.

1
[
2
<parameter at index 0>,
3
<parameter at index 1>,
4
// ...
5
]

Example

1
[
2
{
3
"label": "foo",
4
"id": "bar",
5
"type": {
6
"kind": "String"
7
}
8
}
9
]

Function Types

1
{
2
"kind": "Function",
3
"typeID": "<function name>",
4
"parameters": [
5
<parameter at index 0>,
6
<parameter at index 1>,
7
// ...
8
],
9
"return": <type>
10
}

Example

1
{
2
"kind": "Function",
3
"typeID": "foo",
4
"parameters": [
5
{
6
"label": "foo",
7
"id": "bar",
8
"type": {
9
"kind": "String"
10
}
11
}
12
],
13
"return": {
14
"kind": "String"
15
}
16
}

Reference Types

1
{
2
"kind": "Reference",
3
"authorized": true | false,
4
"type": <type>
5
}

Example

1
{
2
"kind": "Reference",
3
"authorized": true,
4
"type": {
5
"kind": "String"
6
}
7
}

Restricted Types

1
{
2
"kind": "Restriction",
3
"typeID": "<fully qualified type ID>",
4
"type": <type>,
5
"restrictions": [
6
<type at index 0>,
7
<type at index 1>,
8
//...
9
]
10
}

Example

1
{
2
"kind": "Restriction",
3
"typeID": "0x3.GreatContract.GreatNFT",
4
"type": {
5
"kind": "AnyResource",
6
},
7
"restrictions": [
8
{
9
"kind": "ResourceInterface",
10
"typeID": "0x1.FungibleToken.Receiver",
11
"fields": [
12
{
13
"id": "uuid",
14
"type": {
15
"kind": "UInt64"
16
}
17
}
18
],
19
"initializers": [],
20
"type": ""
21
}
22
]
23
}

Capability Types

1
{
2
"kind": "Capability",
3
"type": <type>
4
}

Example

1
{
2
"kind": "Capability",
3
"type": {
4
"kind": "Reference",
5
"authorized": true,
6
"type": {
7
"kind": "String"
8
}
9
}
10
}

Enum Types

1
{
2
"kind": "Enum",
3
"type": <type>,
4
"typeID": "<fully qualified type ID>",
5
"initializers":[],
6
"fields": [
7
{
8
"id": "rawValue",
9
"type": <type>
10
}
11
]
12
}

Example

1
{
2
"kind": "Enum",
3
"type": {
4
"kind": "String"
5
},
6
"typeID": "0x3.GreatContract.GreatEnum",
7
"initializers":[],
8
"fields": [
9
{
10
"id": "rawValue",
11
"type": {
12
"kind": "String"
13
}
14
}
15
]
16
}

Repeated Types

When a composite type appears more than once within the same JSON type encoding, either because it is recursive or because it is repeated (e.g. in a composite field), the composite is instead represented by its type ID.

Example

1
{
2
"type":"Type",
3
"value": {
4
"staticType": {
5
"kind":"Resource",
6
"typeID":"0x3.GreatContract.NFT",
7
"fields":[
8
{"id":"foo",
9
"type": {
10
"kind":"Optional",
11
"type":"0x3.GreatContract.NFT" // recursive NFT resource type is instead encoded as an ID
12
}
13
}
14
],
15
"initializers":[],
16
"type":""
17
}
18
}
19
}