Skip to main content

YAML

Scratchpad 1

yq -n '
            .listeners =[
                {
                    "type": "http",
                    "port": 8080,
                    "tls": false,
                    "x_forwarded": false,
                    "resources": "foo"
                }
            ]'

Output:

listeners:
  - type: http
    port: 8080
    tls: false
    x_forwarded: false
    resources: foo

Alternate:

yq -n '
        .listeners.type = "http" |
        .listeners.port = 8080 |
        .listeners.tls = false |
        .listeners.x-forwarded = false |
        .listeners.bind_addresses = [ "0.0.0.0" ] |
        .listeners.resources = [{"names": ["client", "federation"],"compress" : true}] '
      '

Output:

listeners:
  type: http
  port: 8080
  tls: false
  x-forwarded: false
  bind_addresses:
    - 0.0.0.0
  resources:
    - names:
        - client
        - federation
      compress: true

Scratchpad 2

yq -n '
    .listeners = [
        {
            "type": "http",
            "port": 8080,
            "bind_addresses": [ "0.0.0.0" ] ,
            "tls": false,
            "x_forwarded" : false,
            "resources": {
                    "name":
                            [
                                "client","federation"
                            ],
                            "compress" : true}
        }
        ]'

Output:

listeners:
  - type: http
    port: 8080
    bind_addresses:
      - 0.0.0.0
    tls: false
    x_forwarded: false
    resources:
      name:
        - client
        - federation
      compress: true

Scratchpad 3

yq -n '.a= [{"b": "c","d": "e"}]'

Output:

a:
  - b: c
    d: e

Alternative:

yq -n '.a= [{"b": ["foo","bar"],"d": "e"}]'

Output:

a:
  - b:
      - foo
      - bar
    d: e

Scratchpad 4

yq -n '
        .a.a1 = "value" |
        .a.a2 = "value2" |
        .a.a3 = false |
        .a.aa = [
                {"aaa" :
                        ["aaa1","aaa2"],
                        "ab" : true}]
        '

Output:

a:
  a1: value
  a2: value2
  a3: false
  aa:
    - aaa:
        - aaa1
        - aaa2
      ab: true

Scratchpad 6

    yq -n   '
            .a.a1 = 1234 |
            .a.a2 = false |
            .a.a3 = "string" |
            .a.a4 = true |
            .a.a5 = [ "1.2.3.4" , "5.6.7.8" ] |
            .a.a6 = [
                        { "a6a":
                                    [ "a6aa", "a6ab" ],
                                    "a6b" : false
                        }
                    ]
            '

Output:

a:
  a1: 1234
  a2: false
  a3: string
  a4: true
  a5:
    - 1.2.3.4
    - 5.6.7.8
  a6:
    - a6a:
        - a6aa
        - a6ab
      a6b: false

Scratchpad 7

yq -n   '.a =   {
        "a1" : 1234,
        "a2" : false,
        "a3" : "string",
        "a4" : true,
        "a5" : [ "1.2.3.4" , "5.6.7.8" ] } |
        .a.a6 = ["a6a" ]'

Output:

a:
  a1: 1234
  a2: false
  a3: string
  a4: true
  a5:
    - 1.2.3.4
    - 5.6.7.8
  a6:
    - a6a
    yq -n   '.a =   {
        "a1" : 1234,
        "a2" : false,
        "a3" : "string",
        "a4" : true,
        "a5" : [ "1.2.3.4" , "5.6.7.8" ] } |
        .a.a6 = [ { "a6a" : [ "a6aa" , "a6bb" ] }]'

Output:

a:
  a1: 1234
  a2: false
  a3: string
  a4: true
  a5:
    - 1.2.3.4
    - 5.6.7.8
  a6:
    - a6a:
        - a6aa
        - a6bb

Alternative:

    yq -n   '.a =   {
                    "a1" : 1234,
                    "a2" : false,
                    "a3" : "string",
                    "a4" : true,
                    "a5" : [ "1.2.3.4" , "5.6.7.8" ]
                    } |
                    .a.a6 = [ { "a6a" : [ "a6aa" , "a6bb" ], "a6b" : "false" } ]
                    '

Output:

a:
  a1: 1234
  a2: false
  a3: string
  a4: true
  a5:
    - 1.2.3.4
    - 5.6.7.8
  a6:
    - a6a:
        - a6aa
        - a6bb
      a6b: "false"

{{< /note >}}

Multiline values

export VALUE="one,two,three"
yq -n   '
        .abc = [ "'"$(echo ${VALUE//,/\",\"})"'" ]
        '

Output:

abc:
  - one
  - two
  - three

{{< /note >}}