【问题标题】:jq: Cannot index array with string "id"jq:无法使用字符串“id”索引数组
【发布时间】:2018-09-01 07:19:58
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: arrays json shell jq


    【解决方案1】:

    是一个有趣的野兽。我发现这需要大量的尝试和错误。

    修复 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中的“格式化字符串和转义”

    【讨论】:

      【解决方案2】:

      您的 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
      

      【讨论】:

        【解决方案3】:

        除了这不是有效的 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" }

        【讨论】:

        • 我稍微更新了这个问题,你能重新阅读一下我的编辑吗?很抱歉造成混乱
        • 致任何想知道的人;未来有类似问题的用户,我没有手动编辑我的 .json;我只是改变了它的导出/创建方式,不包含无用的数据。
        猜你喜欢
        • 2016-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-15
        • 1970-01-01
        相关资源
        最近更新 更多