问题描述
我是jq的新手,所以如果这不是jq问题或json问题,请指出正确的方向。我不确定使用正确的术语,因此这使我很难正确表达问题。
I am new to jq so if this is not a jq question or a json question please point me in the right direction. I am not sure of the correct terminology so it is making it hard for me to properly articulate the problem.
我习惯于卷曲拉一些我想要的json筛选出具有特定值的键。以下是一些示例json:
I am using to curl to pull some json that I want to filter out keys with specific values. Here is some of the sample json:
{
"id": "593f468c81aaa30001960e16",
"name": "Name 1",
"channels": [
"593f38398481bc00019632e5"
],
"geofenceProfileId": null
}
{
"id": "58e464585180ac000a748b57",
"name": "Name 2",
"channels": [
"58b480097f04f20007f3cdca",
"580ea26616de060006000001"
],
"geofenceProfileId": null
}
{
"id": "58b4d6db7f04f20007f3cdd2",
"name": "Name 3",
"channels": [
"58b8a25cf9f6e19cf671872f"
],
"geofenceProfileId": "57f53018271c810006000001"
}
当我运行以下命令时:
curl -X GET -H 'authorization: Basic somestring=' "https://myserver/myjson" |
jq '.[] | {id: .id, name: .name, channels: .channels, geofenceProfileId: .geofenceProfileId}' |
jq '.[] | select(.channels == 58b8a25cf9f6e19cf671872f)'
我得到以下错误:
jq:错误:语法错误,意外的IDENT,期望';'或')'(Unix shell引用问题?)在第1行:
。 select(.channels == 58b8a25cf9f6e19cf671872f)
jq:1编译错误
%接收的总百分比Xferd平均速度时间时间时间当前
加载上传的总剩余左速度
100351k 0 351k 0 0 1109k 0-:-:--:-:--:-:-1110k
jq: error: syntax error, unexpected IDENT, expecting ';' or ')' (Unix shell quoting issues?) at , line 1:.[] | select(.channels == 58b8a25cf9f6e19cf671872f)
jq: 1 compile error % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed100 351k 0 351k 0 0 1109k 0 --:--:-- --:--:-- --:--:-- 1110k
是错误是因为jq pretty打印第一条语句的输出,第二条语句期望它在一个代码块中?如果是这样,如何将其转换回非漂亮的打印格式,或者如何使用jq在输出上运行新的过滤器?
Is this error because jq pretty prints the output of the first statement and the second statement is expecting it to be in one code block? If so, how do I convert it back to non pretty print format or how can I use jq to run a new filter on the output?
基本上,我试图解析数百条记录,并过滤掉具有特定通道号或具有特定geofenceProfileId的所有记录。
Basically I am trying to parse hundreds of records and filter out all of the records that are in a specific channel number or have a specific geofenceProfileId.
推荐答案
我建议您开始:
jq 'select(.channels | index("58b8a25cf9f6e19cf671872f"))'
实际上,这可能甚至是您想要的过滤器。如果您要在选择后删除渠道,则可以按如下所示扩大过滤条件:
In fact, this might even be exactly the filter you want. If you want to remove the "channels" once you've made the selection, you could augment the filter above as follows:
select(.channels | index("58b8a25cf9f6e19cf671872f")) | del(.channels)
要注意的主要事情是,可以在一次调用内创建一个管道的所以最有可能的结果是: curl ... | jq ...
The main thing to note is that one can create a pipeline WITHIN a single invocation of jq. So most likely you'll end up with: curl ... | jq ...
jq表达式 { id:.id}
可以缩写为 {id}
,所以代替:
The jq expression {"id": .id}
can be abbreviated to {id}
, so instead of:
{id.id, name: .name, channels: .channels, geofenceProfileId: .geofenceProfileId}
您可以输入:
{id, name, channels, geofenceProfileId}
这篇关于在外壳中处理json(例如curl)时,如何正确地将多个jq语句链接在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!