如何使用jq命令提取具有随机字符串的JSON字段?

时间:2018-04-05 05:59:56

标签: json jq

请耐心等待我,我仍然在学习JSON和编程。所以我有这个JSON文件:

{
    "root_200888546292191": {
        "fields": {
            "buyerId": "31392191"
        },
        "id": "200718546292191",
        "tag": "root",
        "type": "biz"
    },
    "shippingInfo_#package#OF04472002179150#WAREHOUSE_ACCEPTED": {
        "fields": {
            "delivery": {
                "createdAt": "Sen 09 Apr - Rab 11 Apr",
                "desc": "Standar",
                "email": null,
                "method": "Standard",
                "status": "info"
            },
            "statusMap": {
                "active": "Dalam proses",
                "all": ["Dalam proses", "Dalam pengiriman", "Telah diterima"]
            },
            "trackingList": [{
                "info": "Status One",
                "updatedAt": "05 Apr 2018 - 11:00"
            }, {
                "info": "Status Two",
                "updatedAt": "05 Apr 2018 - 11:00"
            }]
        },
        "id": "#package#OF04472002179150#WAREHOUSE_ACCEPTED",
        "tag": "shippingInfo",
        "type": "biz"
    },
    "shippingInfo_#package#AAAAAAAAAAAAA#NOT_WAREHOUSE_ACCEPTED": {
        "fields": {
            "delivery": {
                "createdAt": "Sen 09 Apr - Rab 11 Apr",
                "desc": "Standar",
                "email": null,
                "method": "Standard",
                "status": "info"
            },
            "statusMap": {
                "active": "Dalam proses",
                "all": ["Dalam proses", "Dalam pengiriman", "Telah diterima"]
            },
            "trackingList": [{
                "info": "Status Three",
                "updatedAt": "05 Apr 2018 - 11:00"
            }, {
                "info": "Status Four",
                "updatedAt": "05 Apr 2018 - 11:00"
            }]
        },
        "id": "#package#AAAAAAAAAAAAA#NOT_WAREHOUSE_ACCEPTED",
        "tag": "shippingInfo",
        "type": "biz"
    },
    "login_200718577292191": {
        "fields": {
            "buyerEmail": "myemail@gmail.com",
            "buyerName": "myname"
        },
        "id": "200718522292191",
        "tag": "login",
        "type": "biz"
    }
}

我想在Info>中提取shippingInfo_ fields> trackingList所以我想要的输出是这样的:

Status One
Status Two
Status Three
Status Four

shippingInfo_之后的字符串总是随机的,如何用jq提取它?

据我所知jq '.shippingInfo_*.fields.trackingList.info'

1 个答案:

答案 0 :(得分:0)

直接接近

有许多直接的方法,例如:

使用paths

paths as $p
| select( $p|length == 5 and
         ($p[0] | startswith("shippingInfo_")) and
         $p[1:3] == ["fields", "trackingList"] and
         $p[4] == "info")
| getpath($p)

使用to_entries

to_entries[]
| select(.key |  startswith("shippingInfo_"))
| .value
| .fields.trackingList[]
| .info

间接方法

还有一些值得一提的间接方法,例如

使用辅助函数

def dot(s):
  to_entries[] | select(.key|test(s)) | .value ;

dot("^shippingInfo_")
| .fields.trackingList[]
| .info

最后提到的过滤器可以缩写为:

dot("^shippingInfo_").fields.trackingList[].info

放宽要求

如果忽略“^ shippingInfo_”要求是可以接受的,那么以下内容也值得考虑:

.[].fields.trackingList[]?.info

甚至:

.. | objects.fields.trackingList[]?.info