Tux*_*Sax 4 csv json export-to-csv jq
我正在慢慢掌握jq的工作原理,但距离它还很差。现在,我处于某种情况下设法获得了想要的东西,但没有按照我想要的方式显示它。我敢肯定这很简单,但我很想念...
这是我要解析的JSON示例:
{
"sites": [
{
"site_id": 123456,
"status": "configured",
"domain": "www.domain.com",
"account_id": 654321,
"security": {
"waf": {
"rules": [
{
"action": "block_request",
"action_text": "Block",
"id": "sqli",
"name": "SQLi"
},
{
"action": "block_request",
"action_text": "Block",
"id": "xss",
"name": "XSS"
},
{
"action": "alert",
"action_text": "Alert",
"id": "path_vector",
"name": "Path Vector"
}
]
}
}
}
],
"res": 0,
"res_message": "OK",
"debug_info": {
"id-info": "9123"
}
}
Run Code Online (Sandbox Code Playgroud)
我只需要几个细节,然后将它们以CSV格式输入,这是我到目前为止所做的:
cat test.json | jq -r '.sites [] | [.site_id,.domain],(.security.waf.rules[] | [.action_text]) | @csv'
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
123456,"www.domain.com"
"Block"
"Block"
"Alert"
Run Code Online (Sandbox Code Playgroud)
还不错,但是我正在寻找的东西是这样的:
123456,"www.domain.com","Block","Block","Alert"
Run Code Online (Sandbox Code Playgroud)
结果相同,只显示在一行中。我翻阅手册页,摆弄了一会儿无济于事。是否可以做到?或者我需要其他工具来操作它?
提前致谢!
首先,让我们讨论一下为什么收到此结果。
When you use [] to extract items from objects/arrays, it yields a value for every item in that object/array.
.sites[]
Run Code Online (Sandbox Code Playgroud)
produces a result for the every value in your sites array (which in this case there's only one).
Another thing to note, using a comma (,) will yield the delimited values within that expression.
[.site_id,.domain]
Run Code Online (Sandbox Code Playgroud)
The comma here produces two values, the site_id and the domain. But, those values are collected in to an array (as denoted by the square brackets).
Putting this into the next part of the expression
.security.waf.rules[] | [.action_text]
Run Code Online (Sandbox Code Playgroud)
The first part goes through all the rules objects in that array. Then for each of those objects, creates an array containing the action_text. This creates three arrays (one for each of the rules).
Put this together with the previous part of the expression (slightly reformatted)
([.site_id,.domain]) , (.security.waf.rules[] | [.action_text])
Run Code Online (Sandbox Code Playgroud)
This all together produces four arrays, the array containing the site_id and domain, followed by the three arrays of action_text.
Then for each of those four arrays, a csv row is created giving you the results you see.
So how can we get the desired results?
First, we'll want to start go through all the sites. I'm assuming you want a row per site.
.sites[]
Run Code Online (Sandbox Code Playgroud)
Then for each site, we need to build an array of the values in that row. Start with what we have direct access to.
.site_id, .domain
Run Code Online (Sandbox Code Playgroud)
Then produce the action_text values.
.security.waf.rules[].action_text
Run Code Online (Sandbox Code Playgroud)
Note we're not putting the action_text in a separate array, we just want the value.
Now we put those values together.
.site_id, .domain, (.security.waf.rules[].action_text)
Run Code Online (Sandbox Code Playgroud)
This creates five values as we discussed, but we want to collect them in an array so we may pass it to the @csv filter.
[.site_id, .domain, (.security.waf.rules[].action_text)]
Run Code Online (Sandbox Code Playgroud)
Putting everything together will give us this filter:
.sites[] | [.site_id, .domain, (.security.waf.rules[].action_text)] | @csv
Run Code Online (Sandbox Code Playgroud)
Of course there's many approaches you can take to get these values (like building out arrays separately then combining them) but this is the most direct.
| 归档时间: |
|
| 查看次数: |
92 次 |
| 最近记录: |