分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

[Linux] jq:命令行JSON处理工具

发布时间:2023-09-06 01:09责任编辑:熊小新关键词:Linux

jq命令帮助我们很方便地在终端查看和处理json文件

jq命令的帮助信息:

abby@abby:bgs$ jq -hjq - commandline JSON processor [version 1.5-1-a5b5cbe]Usage: jq [options] <jq filter> [file...] ???jq is a tool for processing JSON inputs, applying the ???given filter to its JSON text inputs and producing the ???filter‘s results as JSON on standard output. ???The simplest filter is ., which is the identity filter, ???copying jq‘s input to its output unmodified (except for ???formatting). ???For more advanced filters see the jq(1) manpage ("man jq") ???and/or https://stedolan.github.io/jq ???Some of the options include: ????-c ???????compact instead of pretty-printed output; ????-n ???????use `null` as the single input value; ????-e ???????set the exit status code based on the output; ????-s ???????read (slurp) all inputs into an array; apply filter to it; ????-r ???????output raw strings, not JSON texts; ????-R ???????read raw strings, not JSON texts; ????-C ???????colorize JSON; ????-M ???????monochrome (don‘t colorize JSON); ????-S ???????sort keys of objects on output; ????--tab ???use tabs for indentation; ????--arg a v ???set variable $a to value <v>; ????--argjson a v ???set variable $a to JSON value <v>; ????--slurpfile a f ???set variable $a to an array of JSON texts read from <f>; ???See the manpage for more options.

使用:

$ jq . xxx.json ??# 直接将文件名传给 jq$ cat bgs_enterprise_info_norm.json | jq . ?#或者由其他命令读出文件内容,并传给 jq

例子:

原始数据:

abby@abby:bgs$ head -n2 bgs_enterprise_info_norm.json{"industry": "纺织服装、服饰业", "name": "xxx公司", "business": "xxxxx", "description": "空值"}{"industry": "批发业", "name": "xxx公司", "business": "xxxxx", "description": "空值"}

格式化:

abby@abby:bgs$ head -n2 bgs_enterprise_info_norm.json | jq .{ ?"industry": "纺织服装、服饰业", ?"name": "xxx公司", ?"business": "xxxxx", ?"description": "空值"}{ ?"industry": "批发业", ?"name": "xxx公司", ?"business": "xxxxx", ?"description": "空值"}

选取某属性:

abby@abby:bgs$ head -n2 bgs_enterprise_info_norm.json | jq .industry"纺织服装、服饰业""批发业"
abby@abby:bgs$ jq .description bgs_enterprise_info_norm.json | grep 空值 | wc -l2770786

参考:

linux 命令之jq

jq : Linux下json的命令行工具

命令行 JSON 处理工具 jq 的使用介绍

[Linux] jq:命令行JSON处理工具

原文地址:http://www.cnblogs.com/bymo/p/7484023.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved