linux json文件怎么打开,使用Bash处理JSON文件

本文介绍了如何在Linux环境下使用Bash脚本和jq工具来处理JSON文件,通过一个具体的例子展示了如何读取和执行JSON文件中定义的任务。示例包括提取JSON数据、检查输入输出文件以及执行相关命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ac70e607ce11f160ba6e0b06f0598185.gif

前言

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。本文提供一个真实的测试用例需求,设计逻辑类似Makefile,我以Bash处理JSON为例,Coding水平有限,请各位多多包涵哈,欢迎大家一起学习和挑战各种不同的语言来实现。

巧用jq处理JSON数据

Test Case

In data pipeline system and configuration management systems, it’s very common that you need execute a bunch of jobs which has dependencies with each other.

Write a program pipeline_runner to execute a list of shell scripts. The definition of those scripts and their dependencies are described in a JSON file. The program only takes in one argument which is the file path of JSON file that defines the jobs.

For example,

// jobs.json

{

"log0_compressed" : {

"commands": "curl http://websrv0/logs/access.log.gz > access0.log.gz",

"input": [],

"output": "access0.log.gz"

},

"log0" : {

"commands": "gunzip access0.log.gz",

"input": ["access0.log.gz"],

"output": "access0.log"

},

"log1_compressed": {

"commands": "curl http://websrv1/logs/access.log.gz > access1.log.gz",

"input": [],

"output": "access1.log.gz"

},

"log1" : {

"commands": "gunzip access1.log.gz",

"input": ["access1.log.gz"],

"output": "access1.log"

},

"log_combined": {

"commands": "cat access0.log access1.log > access.log",

"input": ["access0.log", "access1.log"],

"output": "access.log"

}

}

To run the program

pipeline_runner jobs.json

As you can see, each job has its input files and output files.

A job will only be executed if all its input files exist.

A job can have multiple input files (or none) but only produce one output file.

Users could run the program multiple times, but if a job’s output file already exists, the program would skip the job.

If you’re still not very clear, think of Makefile in Linux systems. The logic is quite similar.

You could complete the test with the programming language you preferred.

Bash Shell

#!/bin/bash

# dos2unix *.sh

# Program:

# This program to test json.

# History:

# 2015/06/18 by OX

#---------------------------- custom variables ---------------------start

runuser=root

# commands

log_combined_commands=`cat jobs.json | ./jq -r '.log_combined.commands'`

log1_commands=`cat jobs.json | ./jq -r '.log1.commands'`

log1_compressed_commands=`cat jobs.json | ./jq -r '.log1_compressed.commands'`

log0_commands=`cat jobs.json | ./jq -r '.log0.commands'`

log0_compressed_commands=`cat jobs.json | ./jq -r '.log0_compressed.commands'`

# input file name

log0_input=`cat jobs.json | ./jq -r '.log0.input[0]'`

log1_input=`cat jobs.json | ./jq -r '.log1.input[0]'`

log_combined_input1=`cat jobs.json | ./jq -r '.log_combined.input[0]'`

log_combined_input2=`cat jobs.json | ./jq -r '.log_combined.input[1]'`

# output file name

log_combined_output=`cat jobs.json | ./jq -r '.log_combined.output'`

log1_output=`cat jobs.json | ./jq -r '.log1.output'`

log1_compressed_output=`cat jobs.json | ./jq -r '.log1_compressed.output'`

log0_output=`cat jobs.json | ./jq -r '.log0.output'`

log0_compressed_output=`cat jobs.json | ./jq -r '.log0_compressed.output'`

#---------------------------- custom variables ---------------------end

#---------------------------- user check ---------------------start

if [ "`whoami`" != "$runuser" ]; then

echo "Please re-run ${this_file} as $runuser."

exit 1

fi

#---------------------------- user check ---------------------end

#---------------------------- function ---------------------start

pause()

{

read -n1 -p "Press any key to continue..."

}

log_combined_check_first()

{

if [ -f "$log_combined_output" ]; then

echo "${log_combined_output} has been generated, the programe will exit"

exit 0

fi

}

log0_compressed_check()

{

if [ ! -f "$log0_compressed_output" ]; then

eval ${log0_compressed_commands}

fi

}

log0_check()

{

if [ ! -f "$log0_output" ]; then

eval ${log0_commands}

fi

}

log1_compressed_check()

{

if [ ! -f "$log1_compressed_output" ]; then

eval ${log1_compressed_commands}

fi

}

log1_check()

{

if [ ! -f "$log1_output" ]; then

eval ${log1_commands}

fi

}

log_combined_check()

{

if [ ! -f "$log_combined_output" ]; then

eval ${log_combined_commands}

echo "${log_combined_output} has been generated, the programe will exit"

fi

}

#---------------------------- function ---------------------end

#---------------------------- main ---------------------start

echo "

Please read first:

[0]Check jobs.json and jq by yourself first

[1]A job will only be executed if all its input files exist.

[2]A job can have multiple input files (or none) but only produce one output file.

[3]Users could run the program multiple times, but if a job's output file already exists, the program would skip the job.

"

pause

#check if file exist and do the job

log_combined_check_first

log0_compressed_check

log0_check

log1_compressed_check

log1_check

log_combined_check

#---------------------------- main ---------------------end

小结

我的代码未实现任意数量jobs的input,希望大牛指点

file://D:\pipeline (2 folders, 4 files, 490.55 KB, 531.04 KB in total.)

│ jobs.json 794 bytes

│ jq 486.13 KB

│ pipeline_runner 2.95 KB

│ README.md 714 bytes

├─logs (0 folders, 2 files, 248 bytes, 248 bytes in total.)

│ access0.log.gz 123 bytes

│ access1.log.gz 125 bytes

└─result (0 folders, 5 files, 40.25 KB, 40.25 KB in total.)

access.log 20.00 KB

access0.log 10.00 KB

access0.log.gz 123 bytes

access1.log 10.00 KB

access1.log.gz 125 bytes

原文:http://wsgzao.github.io/post/bash-json/

### 如何在Linux使用命令行工具编辑JSON文件 #### 使用 `jq` 工具 `jq` 是一款强大的 JSON 处理工具,能够在 Linux 命令行环境中高效地解析、修改和生成 JSON 数据[^1]。通过安装并熟练运用该工具,可以实现对 JSON 文件的各种操作。 要安装 `jq`,可以在基于 Debian 的系统(如 Ubuntu)上运行以下命令: ```bash sudo apt-get update && sudo apt-get install jq ``` 对于 Red Hat 或 CentOS 系统,则可执行如下命令完成安装: ```bash sudo yum install epel-release && sudo yum install jq ``` 一旦成功安装了 `jq`,就可以利用其功能来编辑 JSON 文件。例如,假设有一个名为 `data.json` 的文件,其中包含以下内容: ```json { "name": "Alice", "age": 25, "city": "New York" } ``` 如果想更改字段 `"age"` 的值为 `30` 并保存到新文件中,可以这样操作: ```bash jq '.age = 30' data.json > new_data.json ``` 为了直接覆盖原文件的内容,可以通过管道符结合其他命令达成目标: ```bash jq '.age = 30' data.json | sponge data.json ``` 这里使用的 `sponge` 来自 GNU `moreutils` 软件包,用于缓冲标准输入直到读取结束再写入文件,从而安全地更新原始文件[^2]。 另外,还可以删除某些键或者添加新的键值对。比如移除 `"city"` 字段以及新增一个叫 `"job"` 的键及其对应的字符串值 `"Engineer"`: ```bash jq 'del(.city) | . += {"job":"Engineer"}' data.json > updated_data.json ``` 以上展示了如何借助 `jq` 这一优秀的命令行工具,在不离开终端的情况下灵活地调整 JSON 文档中的各项属性。 #### 其他可能的选择 除了 `jq`,还有其他的命令行工具可用于处理 JSON 数据,像 Python 结合 `-c` 参数调用内置库 json 实现快速脚本编写;Perl 和 Ruby 同样具备相应的模块支持这样的任务需求。不过这些方法通常较之专门设计的 `jq` 更加冗长复杂一些[^2]。 ```python import sys, json obj = json.load(sys.stdin) obj['new_key'] = 'value' print(json.dumps(obj)) ``` 此段代码片段演示了一个简单的例子——从标准输入接收 JSON 对象后增加一个新的键值对然后再打印出来。虽然这需要更多编程背景知识才能有效应用,但对于熟悉相应语言的人来说也是一种可行方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值