如何在 bash 中将 json 响应转换为 yaml [英] How to convert a json response into yaml in bash

查看:40
本文介绍了如何在 bash 中将 json 响应转换为 yaml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 jq 从 json 文件中读取数据.我想将结果附加到一个 yaml 文件中,但我没有让它工作.我对 shell 编程很陌生.我的目标是将用户"附加到 yaml 文件中现有的用户"数组中.

I read data from a json file with jq. I wanna append the results into a yaml file, but I dont get it working. I am quite new to shell programming. My goal is to append that "users" to an existing "users"-Array in a yaml file.

这是我的 json 文件:

This is my json file:

#$DEFAULTS_FILE

{"users":
  [
    {"name":"pi",
      "gecos": "Hypriot Pirate",
      "sudo":"ALL=(ALL) NOPASSWD:ALL",
      "shell": "/bin/bash",
      "groups":"users,docker,video",
      "plain_text_passwd":"pi",
      "lock_passwd":"false",
      "ssh_pwauth":"true",
      "chpasswd": {"expire": false}
    },
    {"name":"admin",
      "gecos": "Hypriot Pirate",
      "sudo":"ALL=(ALL) NOPASSWD:ALL",
      "shell": "/bin/bash",
      "primary-group": "users",
      "groups":"users,docker,adm,dialout,audio,plugdev,netdev,video",
      "ssh-import-id":"None",
      "plain_text_passwd":"pi",
      "lock_passwd":"true",
      "ssh_pwauth":"true",
      "chpasswd": "{expire: false}",
      "ssh-authorized-keys": ["ssh-rsa abcdefg1234567890 YOUR_KEY@YOURHOST.local"]
    }
  ]
  }

我用那个过滤它:

cat $DEFAULTS_FILE |jq .users

我不知道如何将该 json 转换为 yaml.

I have no clue how to convert that json into a yaml.

我的预期结果应该是:

users:
  - name:                pi
    gecos:               "Hypriot Pirate"
    sudo:                ALL=(ALL) NOPASSWD:ALL
    shell:               /bin/bash
    groups:              users,docker,video
    plain_text_passwd:   pi
    lock_passwd:         false
    ssh_pwauth:          true
    chpasswd: { expire:  false }
  - name:                admin
    primary-group:       users
    shell:               /bin/bash
    sudo:                ALL=(ALL) NOPASSWD:ALL
    groups:              users,docker,adm,dialout,audio,plugdev,netdev,video
    ssh-import-id:       None

我尝试使用第二个工具 yq,它类似于 jq,可以编写 yaml 文件.但我没有积极的进展.

I tried to use a second tool called yq which is similar to jq and can write yaml files. But I have no positive progress.

编辑

我知道我可以将内容添加到 yaml 中:

I know that I can add content to the yaml with that:

yq w -i "my.yml" "users[+]" "一些内容"

但我不知道如何将我的 json 合并到其中.

But I dont know how to merge my json into that.

任何帮助或提示都会很好,在此先感谢您...

Any help or hint would be nice, thank you in advance...

推荐答案

yq jq

使用 yq 版本 4.8.0:

cat $DEFAULTS_FILE |yq e -P -

  • eeval 分别处理文件.eaeval-all 将首先合并文件.
  • -P--prettyPrint YAML 输出
  • - 来自标准输入
  • e or eval handles file separately. ea or eval-all will merge files first.
  • -P or --prettyPrint YAML output
  • - from STDIN

注意:你也可以采用其他方式(yaml 到 json)yq e -j file.yaml

使用 yq 版本 3.3.2:

cat $DEFAULTS_FILE |yq r -P -

  • r 读取
  • -P --prettyPrint
  • - 来自标准输入
  • r read
  • -P --prettyPrint
  • - from STDIN

这篇关于如何在 bash 中将 json 响应转换为 yaml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆