gpt4 book ai didi

json - 将 JSON 对象转换为 URL 查询字符串的命令行

转载 作者:行者123 更新时间:2023-12-05 05:10:55 25 4
gpt4 key购买 nike

我正在寻找将 JSON 对象转换为 URL 查询字符串的 CLI:

{
"action_name": "Example",
"idsite": 1,
"rec": 1,
"r": 345007,
"h": 22,
"m": 58,
"s": 47,
"url": "http://example.com",
"_id": "44ebfeb79a5bb5e6",
"_idts": 1539806328,
"_idvc": 1,
"_idn": 0,
"_refts": 0,
"_viewts": 1539806328,
"send_image": 1,
"cookie": 1,
"res": "412x732",
"gt_ms": 266,
"pv_id": "bQkkXv"
}

结果会是这样的:

curl -Gi 'http://127.0.0.1:8000/piwik.php?action_name=Example&idsite=1&rec=1&r=345007&h=22&m=58&s=47&url=http%3A%2F%2Fexample.com%2F&_id=44ebfeb79a5bb5e6&_idts=1539806328&_idvc=1&_idn=0&_refts=0&_viewts=1539806328&send_image=1&cookie=1&res=412x732&gt_ms=266&pv_id=bQkkXv'

最佳答案

您可以使用 jq使用以下过滤器

[to_entries[] | (@uri "\(.key)"+ "="+ @uri "\(.value)")] |加入(“&”)

$ jq -r '[to_entries[] | (@uri "\(.key)" + "=" + @uri "\(.value)")] | join("&")' << JSON
{
"var1":"value 1",
"another var":"Some value with special & = [ ] chars"
}
JSON
var1=value%201&another%20var=Some%20value%20with%20special%20%26%20%3D%20%5B%20%5D%20chars

观看直播 https://jqplay.org/s/lu0TAWF2HHf

过滤空值/空值

在 jq 过滤器前加上 with_entries(select(.value | length !=0)) |

https://jqplay.org/s/6jQpPs5EepJ

关于json - 将 JSON 对象转换为 URL 查询字符串的命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56024033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com