机器人 救救瓜
Toggle navigation
Home
SCM-tool
Linux
Jenkins
SVN
other
About Me
Archives
Tags
Linux Shell 处理 json 对象 (分解 gitlab API 返回的json 对象)-- jq
? shell ?
? gitlab ?
2019-09-18 10:38:35
658
0
0
gua_l
? shell ?
? gitlab ?
#### JQ URL https://stedolan.github.io/jq/download/ https://stedolan.github.io/jq/manual/#Invokingjq 这是一个开源工具,用于处理json 。 ## 前言 > "The API uses JSON to serialize data. You don’t need to specify .json at the end of an API URL." > 在linux 下使用gitlab API 返回json 对象,经常使用python处理返回结果。 如果只是单纯地使用shell脚本中,常将其当作文本,并用sed ,grep,cut分解。但是因为它不是准确地分解对象中的组成,而是简单地通过关键字进行筛选和切割,所以经常会有遗漏或不够准确。 在linux 中安装jq , 可以在shell 中直接使用jq 对json 对象进行处理。 ## 安装jq ###### 下载 jq 源码包编译安装 ``` # configure ,make ,make install cd jq autoreconf -i ./configure --disable-maintainer-mode make sudo make install ``` ## 下载二进制文件直接使用 ``` [root@huangbei ~]# chmod +x jq-linux64 [root@huangbei ~]# mv jq-linux64 /usr/bin/jq [root@huangbei ~]# jq jq - commandline JSON processor [version 1.6] Usage: jq [options] <jq filter> [file...] jq [options] --args <jq filter> [strings...] jq [options] --jsonargs <jq filter> [JSON_TEXTS...] 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 copies jq's input to its output unmodified (except for formatting, but note that IEEE754 is used for number representation internally, with all that that implies). For more advanced filters see the jq(1) manpage ("man jq") and/or https://stedolan.github.io/jq Example: $ echo '{"foo": 0}' | jq . { "foo": 0 } For a listing of options, use jq --help. ``` Example ``` List_project_ISSUE_JSON=$(curl --header "PRIVATE-TOKEN: $P_token" ${GITLAB_INSTANCE}/api/${API_VERSION}/projects/${Project_ID}/issues?state=${Status} -k 2>/dev/null) ISSUE_ID=$(echo $List_project_ISSUE_JSON | jq '.[].iid') ```
Pre:
Git 的review 方式 : gitlab 的 Merge request 和 gerrit
Next:
shell 字符串长度
0
likes
658
Weibo
Wechat
Tencent Weibo
QQ Zone
RenRen
Submit
Sign in
to leave a comment.
No Leanote account?
Sign up now.
0
comments
More...
Table of content
No Leanote account? Sign up now.