04-23 周二 shell环境下读取使用jq 读取json文件

本文介绍了如何在Linuxshell环境中利用jq工具处理JSON和YAML文件,展示了如何从`config.json`文件中提取用户、密码、URL和标签数组。作者通过示例展示了如何遍历`ci_repositories`并输出相关信息。
摘要由CSDN通过智能技术生成
04-23 周二 shell环境下读取使用jq 读取json文件
时间版本修改人描述
04-23V0.1宋全恒新建文档

简介

工具列表

Shell脚本处理JSON数据工具jq

jshon是另外一个读取json数据的工具 而且其支持XML和YAML格式文件

linux shell环境下处理yml文件

#!/bin/bash
 
# 加载shyaml库
. /usr/local/bin/shyaml.sh
 
# 读取YAML文件
mapfile -t yaml_content < config.yaml
 
# 解析YAML内容
eval $(shyaml parse <<<"${yaml_content[*]}")
 
# 使用解析出来的变量
echo $name
echo ${servers[0]}

[如何在shell脚本中从yaml文件读取特定数据](javascript:void(0)😉

看来要用shyaml 和yq来进行参数的读取。

  • json
    • jq
    • jshon
  • yaml文件
    • shyaml
    • yq

使用cat和jq演示配置文件的读取

配置文件config.json

ci_repositories:
  - user: "user1"
    passwd: "password1"
    url: "http://ci.example.com/repository1"
    tags:
      - "tag1"
      - "tag2"
      - "tag3"
  - user: "user2"
    passwd: "password2"
    url: "http://ci.example.com/repository2"
    tags:
      - "tag4"
      - "tag5"
  - user: "user3"
    passwd: "password3"
    url: "http://ci.example.com/repository3"
    tags: []

注: 上述展示了一个yaml文件,在下面程序运行时,应该将其转化为json文件。

yangfei@ubuntu:/home/songquanheng/json$ cat config.json 
{
  "ci_repositories": [
    {
      "user": "user1",
      "passwd": "password1",
      "url": "http://ci.example.com/repository1",
      "tags": [
        "tag1",
        "tag2",
        "tag3"
      ]
    },
    {
      "user": "user2",
      "passwd": "password2",
      "url": "http://ci.example.com/repository2",
      "tags": [
        "tag4",
        "tag5"
      ]
    },
    {
      "user": "user3",
      "passwd": "password3",
      "url": "http://ci.example.com/repository3",
      "tags": []
    }
  ]
}

程序脚本

#!/bin/bash
  
# 读取 JSON 文件并遍历 ci_repositories
jq -c '.ci_repositories[]' config.json | while IFS= read -r repository; do
    # 从每个 repository 中提取 user、passwd、url 和 tags
    User=$(echo "$repository" | jq -r '.user')
    Passwd=$(echo "$repository" | jq -r '.passwd')
    Url=$(echo "$repository" | jq -r '.url')

    # 将 Tags 数组存储为 Shell 数组变量
    Tags=$(echo "$repository" | jq -r '.tags | join(" ")')
    read -r -a TagsArray <<<"$Tags"

    # 获取 TagsArray 数组的长度
    TagsLength=${#TagsArray[@]}

    # 获取 TagsArray 数组的第一个元素
    Tag1=${TagsArray[0]}

    # 打印变量值(可替换为你想要的处理逻辑)
    echo "User: $User"
    echo "Passwd: $Passwd"
    echo "Url: $Url"
    echo "Tags: ${TagsArray[@]}"
    echo "Tags Length: $TagsLength"
    echo "Tag at index 1: $Tag1"
    echo "------------------------"
done

代码解析

 代码解析

image-20240423143052301

shell执行结果

 上述的代码,在执行时输出了如下的结果

songquanheng@ubuntu:~/json$ bash b.sh 
User: user1
Passwd: password1
Url: http://ci.example.com/repository1
Tags: tag1 tag2 tag3
Tags Length: 3
Tag at index 1: tag1
------------------------
User: user2
Passwd: password2
Url: http://ci.example.com/repository2
Tags: tag4 tag5
Tags Length: 2
Tag at index 1: tag4
------------------------
User: user3
Passwd: password3
Url: http://ci.example.com/repository3
Tags: 
Tags Length: 0
Tag at index 1: 

总结

 上文演示了使用jq读取yaml的配置文件,并且处理了解读过程中的配置文件。通过使用在shell中使用jq命令,我们可以非常方便的进行配置文件的读取。非常的方便。

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值