代码之家  ›  专栏  ›  技术社区  ›  nbari

从引用的嵌套对象中获取与键值匹配的对象

  •  1
  • nbari  · 技术社区  · 6 年前

    我有一个JSON对象列表

    [
     {
        "uuid": "2e08c71c-56af-4c1d-9677-0aad7bf38536",
        "name": "img-s3",
        "summary": "",
        "retention_name": "month",
        "retention_uuid": "beae2f1a-10e9-4b53-b9b1-baf823624ff2",
        "expiry": 2678400,
        "schedule_name": "daily",
        "schedule_uuid": "baca1a64-b506-4789-83c8-653573f2b99f",
        "schedule_when": "daily 2am",
        "paused": false,
        "store_uuid": "d91fd7b6-4c39-4f73-aa44-8ec55f52da65",
        "store_name": "default",
        "store_plugin": "s3v8",
        "store_endpoint": "{\"access_key_id\":\"xxxx\",\"bucket\":\"images-c4\",\"s3_host\":\"s3.amazon.com\",\"secret_access_key\":\"yyy\",\"signature_version\":\"2\",\"skip_ssl_validation\":true}",
        "target_uuid": "991c7aa2-cdd3-4f90-bb28-1431c99f8fc8",
        "target_name": "sc-asd",
        "target_plugin": "mysql",
        "target_endpoint": "{\"mysql_database\":\"users\",\"mysql_host\":\"10.10.22.49\",\"mysql_password\":\"password\",\"mysql_port\":\"3306\",\"mysql_user\":\"user\"}",
        "agent": "127.0.0.1:3030"
    }
    ...
    ]
    

    我只需要使用 storege_plugin: "s3v8" 在对象的“引用”值中 storage_endpoint 钥匙 signature_version 2

    发现物体有 storage_plugin: "s3v8" ,和 store_endpoing != null 我用这个:

    $ command | jq '.[] | select(.store_plugin == "s3v8" and .store_endpoint != null)'
    

    但我现在的问题是 store_endpoint 我只需要根据一个键的值得到结果,在这种情况下, signature_value

    要测试如何取消引用并搜索值,我尝试以下操作:

    $ echo "{\"access_key_id\":\"xxxx\",\"bucket\":\"images-c4\",\"s3_host\":\"s3.amazon.com\",\"secret_access_key\":\"yyy\",\"signature_version\":\"2\",\"skip_ssl_validation\":true}" \
    | jq -r 'select(.signature_version == "2")'
    {
      "access_key_id": "xxxx",
      "bucket": "images-c4",
      "s3_host": "s3.amazon.com",
      "secret_access_key": "yyy",
      "signature_version": "2",
      "skip_ssl_validation": true
    }
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   Inian    6 年前

    你需要使用 fromjson 在嵌套的JSON字段上构造,如下所示。这个 fromjson

    .[] | select((.store_plugin == "s3v8" and .store_endpoint != null) and (.store_endpoint | fromjson | .signature_version == "2") )
    

    jqplay.org - URL