在 jq 输出中包含空 JSON 值
Including empty JSON values in jq output
我正在尝试获取包含偶尔为空值的 .csv。
调用此 API (https://www.campaignmonitor.com/api/subscribers/#getting-subscribers-details) 我得到以下信息:
[
{
"ID": "fc0ce7105baeaf97f47c99be31d02a91",
"Type": "Campaign",
"Name": "Campaign One",
"Actions": [
{
"Event": "Open",
"Date": "2010-10-12 13:18:00",
"IPAddress": "192.168.126.87",
"Detail": ""
},
{
"Event": "Click",
"Date": "2010-10-12 13:16:00",
"IPAddress": "192.168.126.87",
"Detail": "https://example.com/post/12323/"
}
]
}
{
"ID": "dsadsamdkl9309ujd432",
"Type": "Campaign",
"Name": "Campaign Two",
"Actions": []
}
]
我想得到的输出:
"Campaign One","Open"
"Campaign One","Click"
"Campaign Two","none"
我目前得到的
"Campaign One","Open"
"Campaign One","Click"
当 "Actions" == []
时,我似乎找不到包含值的方法
到目前为止我尝试了什么:
尝试 1:
curl -u "apikey:x" https://api.createsend.com/api/v3.2/subscribers/listID/history.json?email=example@email.com | jq -r '.[] | .Name as $n | .Actions[] | ([$n, .Event | if . == null then "none" else . end]) | @csv'
尝试 2:
curl -u "apikey:x" https://api.createsend.com/api/v3.2/subscribers/listID/history.json?email=example@email.com | jq -r '.[] | .Name as $n | .Actions[] | ([$n, .Event // "none"]) | @csv'
尝试 3:
curl -u "apikey:x" https://api.createsend.com/api/v3.2/subscribers/listID/history.json?email=example@email.com | jq -r '.[] | .Name as $n | .Actions[] |.Actions[] | if . == [] then .Actions[].Event = "" else . end | ([$n, .Event]) | @csv'
jq -r '.[] | (.Actions[].Event // "none") as $e | [ .Name, $e ] | @csv'
这假定第 20 行缺少的逗号已被插入。
我正在尝试获取包含偶尔为空值的 .csv。
调用此 API (https://www.campaignmonitor.com/api/subscribers/#getting-subscribers-details) 我得到以下信息:
[
{
"ID": "fc0ce7105baeaf97f47c99be31d02a91",
"Type": "Campaign",
"Name": "Campaign One",
"Actions": [
{
"Event": "Open",
"Date": "2010-10-12 13:18:00",
"IPAddress": "192.168.126.87",
"Detail": ""
},
{
"Event": "Click",
"Date": "2010-10-12 13:16:00",
"IPAddress": "192.168.126.87",
"Detail": "https://example.com/post/12323/"
}
]
}
{
"ID": "dsadsamdkl9309ujd432",
"Type": "Campaign",
"Name": "Campaign Two",
"Actions": []
}
]
我想得到的输出:
"Campaign One","Open"
"Campaign One","Click"
"Campaign Two","none"
我目前得到的
"Campaign One","Open"
"Campaign One","Click"
当 "Actions" == []
到目前为止我尝试了什么:
尝试 1:
curl -u "apikey:x" https://api.createsend.com/api/v3.2/subscribers/listID/history.json?email=example@email.com | jq -r '.[] | .Name as $n | .Actions[] | ([$n, .Event | if . == null then "none" else . end]) | @csv'
尝试 2:
curl -u "apikey:x" https://api.createsend.com/api/v3.2/subscribers/listID/history.json?email=example@email.com | jq -r '.[] | .Name as $n | .Actions[] | ([$n, .Event // "none"]) | @csv'
尝试 3:
curl -u "apikey:x" https://api.createsend.com/api/v3.2/subscribers/listID/history.json?email=example@email.com | jq -r '.[] | .Name as $n | .Actions[] |.Actions[] | if . == [] then .Actions[].Event = "" else . end | ([$n, .Event]) | @csv'
jq -r '.[] | (.Actions[].Event // "none") as $e | [ .Name, $e ] | @csv'
这假定第 20 行缺少的逗号已被插入。