JSON

How CUE integrates with the JSON data-interchange format.

Intro

CUE is a superset of JSON: any valid JSON file is a valid CUE file. There is not much more integration you can get than that. The main motivation to make it a superset was to promote familiarity.

Generate JSON

The cue export command outputs CUE in a data format. By default this is JSON.

Given the following file

$ cat >> coordinates.cue << EOF
x: 43.45
y: -34.12
EOF

$ cue export coordinates.cue
{
    "x": 43.45,
    "y": -34.12
}

JSON in CUE

It may also be needed to deal with JSON inside a CUE file. For instance, a Kubernetes ConfigMap may specify an embedded JSON file. CUE provides a builtin JSON package to parse, generate, or validate JSON from within CUE.

Create

The builtin encoding/json.Marshal generates JSON from within CUE.

import "encoding/json"

configMap data "point.json":
    json.Marshal({
        x: 4.5
        y: 2.34
    })
{
    "configMap": {
        "data": {
            "point.json": "{\"x\":4.5,\"y\":2.34}"
        }
    }
}

Parse

The reverse is also possible

import "encoding/json"

data:  #"{"x":4.5,"y":2.34}"#
point: json.Unmarshal(data)
{
    "data": "{\"x\":4.5,\"y\":2.34}",
    "point": {
        "x": 4.5,
        "y": 2.34
    }
}

Validate

// dim.cue
import "encoding/json"

Dimensions :: {
    width:  number
    depth:  number
    height: number
}

// Validate JSON configurations embedded strings.
configs <_>: json.Validate(Dimensions)

configs bed:      #"{ "width": 2, "height": 0.1, "depth": 2 }"#
configs table:    #"{ "width": "34", "height": 23, "depth": 0.2 }"#
configs painting: #"{ "width": 34, "height": 12, "depht": 0.2 }"#
$ cue vet dim.cue
configs.table: error in call to encoding/json.Validate: conflicting values number and "34" (mismatched types number and string):
    ./dim.cue:11:14
configs.painting: error in call to encoding/json.Validate: field "depht" not allowed in closed struct:
    ./dim.cue:11:14
Last modified September 5, 2019: Remove unnecessary char (1a1067e)