gpt4 book ai didi

arrays - jq: 无法用字符串 "id"索引数组

转载 作者:行者123 更新时间:2023-12-04 16:17:16 24 4
gpt4 key购买 nike

我的 backup.json 看起来像这样:

{
"andres": [
[
{
"id": 1,
"email": "password",
"username": test1,
"password": "email@email.com",
"name": "Dummy Account",
"address": "123 st road",,
"ip_address": "0.0.0.0",
"phone": "123-123-1234",
},
{
"id": 2,
"email": "email2@email.com",
"username": test2,
"password": "password",
"name": "Dummy Account",
"address": "123 st road",,
"ip_address": "0.0.0.0",
"phone": "123-123-1234"
}
],
]
}

我正在使用命令:

jq -r '.andres[] | .id, .email, .username, .password, .name, .address, .ip_address, .phone' < backup.json > backup.csv

但它给出了错误:

Cannot index array with string "id"

我希望它看起来像这样:

1,email@email.com,test1,password,Dummy Account,123 st road,0.0.0.0,123-123-1234
2,email@email.com,test2,password,Dummy Account,123 st road,0.0.0.0,123-123-1234

我刚开始使用 JQ。有人可以修复我的命令并告诉我哪里出错了吗?

谢谢!

最佳答案

您的 json 中 andres 的值是一个数组数组,但您访问它时就好像它是一个对象数组一样。您必须先展平数组(或索引)才能访问对象。然后从这些对象中,您需要将所需的值映射为 csv 值数组。

$ jq -r '
.andres[][] | [.id, .email, .username, .password, .name, .address, .ip_address, .phone] | @csv
' < backup.json > backup.csv

注意 .andres[][] 中的第二组 []

您可能还想在输出中添加一些 header 。

$ jq -r '
["id", "email", "username", "password", "name", "address", "ip_address", "phone"] as $headers
| $headers, (.andres[][] | [.[$headers[]]]) | @csv
' < backup.json > backup.csv

关于arrays - jq: 无法用字符串 "id"索引数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52125879/

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