请启用 Javascript 以查看内容

yaml格式和json格式文件互相转换

 ·   ·  ☕ 2 分钟  ·  ✍️ ayunw

前言

工作中,很多的场景下都需要将 yaml 和 json 格式的文件互换,一般我们有两种方式进行转换:一种是使用 yq 命令(有时候也会配合jq命令),另一种方法就是通过使用 Python 来实现。这里记录一下使用yq命令解析方式,适当的时候使用jq命令配合的实现方法。

yaml 文件示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ cat test.yaml
apiVersion: gateway.solo.io/v1
kind: VirtualService
metadata:
  name: ayunw-gateway-sit-8080-external
  namespace: ayunw
  labels:
    appEnv: sit
    appName: ayunw-gateway
spec:
  virtualHost:
    domains:
      - www.sit.ayunw.cn
      - www.perf.ayunw.cn
    routes:
      - matchers:
        - prefix: /
        delegateAction:
          selector:
            namespaces:
            - ayunw
            expressions:
              - key: "appEnv"
                operator: "Equals"
                values:
                - "sit"
              - key: "appName"
                operator: "Equals"
                values:
                - "ayunw-gateway"
              - key: "appPort"
                operator: "Equals"
                values:
                - "8080"

yq 命令

1、yq 命令安装

1
2
3
4
5
6
$ wget https://github.com/mikefarah/yq/releases/download/v4.26.1/yq_linux_amd64 \
  && chmod +x yq_linux_amd64 \
  && mv yq_linux_amd64 /usr/local/bin/yq

$ yq --version
yq (https://github.com/mikefarah/yq/) version 4.26.1

2、yaml 转 json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
$ yq eval -o json test.yaml | tee test.json
{
  "apiVersion": "gateway.solo.io/v1",
  "kind": "VirtualService",
  "metadata": {
    "name": "ayunw-gateway-sit-8080-external",
    "namespace": "ayunw",
    "labels": {
      "appEnv": "sit",
      "appName": "ayunw-gateway"
    }
  },
  "spec": {
    "virtualHost": {
      "domains": [
        "www.sit.ayunw.cn",
        "www.perf.ayunw.cn"
      ],
      "routes": [
        {
          "matchers": [
            {
              "prefix": "/"
            }
          ],
          "delegateAction": {
            "selector": {
              "namespaces": [
                "ayunw"
              ],
              "expressions": [
                {
                  "key": "appEnv",
                  "operator": "Equals",
                  "values": [
                    "sit"
                  ]
                },
                {
                  "key": "appName",
                  "operator": "Equals",
                  "values": [
                    "ayunw-gateway"
                  ]
                },
                {
                  "key": "appPort",
                  "operator": "Equals",
                  "values": [
                    "8080"
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
}

这样就将yaml格式的文件转为json格式的了,输出到控制台同时保存到了test.json 文件。

3、 json 转 yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ yq eval -P test.json | tee test.yaml
apiVersion: gateway.solo.io/v1
kind: VirtualService
metadata:
  name: ayunw-gateway-sit-8080-external
  namespace: ayunw
  labels:
    appEnv: sit
    appName: ayunw-gateway
spec:
  virtualHost:
    domains:
      - www.sit.ayunw.cn
      - www.perf.ayunw.cn
    routes:
      - matchers:
          - prefix: /
        delegateAction:
          selector:
            namespaces:
              - ayunw
            expressions:
              - key: appEnv
                operator: Equals
                values:
                  - sit
              - key: appName
                operator: Equals
                values:
                  - ayunw-gateway
              - key: appPort
                operator: Equals
                values:
                  - "8080"

注意: 4.x 版本的 yq 命令和 3.x 版本的 yq 命令使用方式天差地别,并且不向前兼容。因此不同版本解析的方式是不同的。

比如: 3.4.0 版本的 yq 命令,配合 jq 命令,解析方法如下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
$ wget https://github.com/mikefarah/yq/releases/download/3.4.0/yq_linux_amd64 \
  && chmod +x yq_linux_amd64 \
  && mv yq_linux_amd64 /usr/local/bin/yq3.4

$ wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 \
  && chmod +x jq-linux64
  && mv jq-linux64 /usr/local/bin/jq

$ yq3.4 -V
yq version 3.4.0

$ jq -V
jq-1.6

$ yq3.4 r -j test.yaml
{"apiVersion":"gateway.solo.io/v1","kind":"VirtualService","metadata":{"labels":{"appEnv":"sit","appName":"ayunw-gateway"},"name":"ayunw-gateway-sit-8080-external","namespace":"ayunw"},"spec":{"virtualHost":{"domains":["www.sit.ayunw.cn","www.perf.ayunw.cn"],"routes":[{"delegateAction":{"selector":{"expressions":[{"key":"appEnv","operator":"Equals","values":["sit"]},{"key":"appName","operator":"Equals","values":["ayunw-gateway"]},{"key":"appPort","operator":"Equals","values":["8080"]}],"namespaces":["ayunw"]}},"matchers":[{"prefix":"/"}]}]}}}


$ yq3.4 r -j test.yaml | jq .
{
  "apiVersion": "gateway.solo.io/v1",
  "kind": "VirtualService",
  "metadata": {
    "labels": {
      "appEnv": "sit",
      "appName": "ayunw-gateway"
    },
    "name": "ayunw-gateway-sit-8080-external",
    "namespace": "ayunw"
  },
  "spec": {
    "virtualHost": {
      "domains": [
        "www.sit.ayunw.cn",
        "www.perf.ayunw.cn"
      ],
      "routes": [
        {
          "delegateAction": {
            "selector": {
              "expressions": [
                {
                  "key": "appEnv",
                  "operator": "Equals",
                  "values": [
                    "sit"
                  ]
                },
                {
                  "key": "appName",
                  "operator": "Equals",
                  "values": [
                    "ayunw-gateway"
                  ]
                },
                {
                  "key": "appPort",
                  "operator": "Equals",
                  "values": [
                    "8080"
                  ]
                }
              ],
              "namespaces": [
                "ayunw"
              ]
            }
          },
          "matchers": [
            {
              "prefix": "/"
            }
          ]
        }
      ]
    }
  }
}

注意: 配合 jq 命令能够将 yaml 解析成 json 格式输出,jq 的版本相对更新慢, yq 版本更新速度特别快。

您的鼓励是我最大的动力
alipay QR Code
wechat QR Code

Avatar
作者
ayunw
尼古丁的绑架没有救赎,我们皆是上瘾的囚徒


目录