jq:无法索引具有字符串“ id”的数组

时间:2018-09-01 07:19:59

标签: arrays json shell jq

我的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的新手。有人可以修正我的命令并告诉我我哪里出问题了吗?

谢谢!

3 个答案:

答案 0 :(得分:2)

是有趣的野兽。我发现这需要大量的反复试验。

修复JSON后

$ jq -r '.andres[][] | map(values) | @csv' file.json
1,"password","test1","email@email.com","Dummy Account","123 st road","0.0.0.0","123-123-1234"
2,"email2@email.com","test2","password","Dummy Account","123 st road","0.0.0.0","123-123-1234"

请注意,对于id = 1,您已切换了密码和电子邮件值。

另请参见the jq manual中的“设置字符串格式和转义格式”

答案 1 :(得分:1)

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

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

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

您可能还希望在输出中添加一些标题。

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

答案 2 :(得分:-1)

除了这不是无效的json(,处还有其他"phone": "123-123-1234",),但是将其删除后,我可以在此处获得有效的json。
您的json文件中还有更多的数组[ { .. } ],您需要使用[][][]将此数组考虑在内。
以下json:

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

使用以下过滤器:

 .Emails[][][].andres[][] | .id, .email, .username, .password, .name, .address, .ip_address, .phone

给我:

jq: error (at <stdin>:34): Cannot iterate over boolean (true)
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"
exit status 5

jqplay上。我不知道该如何处理json中的true,它是无效的,它应该是数组成员,例如{ "true" }