如何使用jq搜索包含特定字符串的JSON密钥?

时间:2018-03-29 22:18:32

标签: json regex key jq

我有一个JSON数据:

{
    "orderTotal_1": {
        "fields": {
            "button": {
                "actionDialog": null,
                "actionUrl": null,
                "clicked": false,
                "enable": true,
                "text": "LANJUTKAN KE PEMBAYARAN",
                "textColor": null
            },
            "payment": {
                "pay": "Rp1.953.800",
                "taxTip": "Termasuk PPN, jika berlaku.",
                "title": "Total"
            },
            "timestamp": 1522355946093
        },
        "id": "1",
        "tag": "orderTotal",
        "type": "biz"
        },
    "rightContainer_10010": {
        "fields": {
            "css": {
                "backgroundColor": null,
                "floatPosition": "right",
                "marginTop": null,
                "width": "388px"
            }
        },
        "id": "10010",
        "tag": "rightContainer",
        "type": "container"
    },
    "toPayBtn_10021": {
        "fields": {
            "clicked": false,
            "enable": true,
            "text": "LANJUTKAN KE PEMBAYARAN"
        },
        "id": "10021",
        "tag": "toPayBtn",
        "type": "biz"
    },
    "voucherInput_1": {
        "fields": {
            "buttonText": "GUNAKAN",
            "placeHolder": "Masukkan Kode Voucher",
            "status": "default"
        },
        "id": "1",
        "tag": "voucherInput",
        "type": "biz"
    }
}

我想获得toPayBtn_10021,但数字10021是动态的。所以它可以是toPayBtn_34 toPayBtn_21等等。

这是我想要实现的输出:

"toPayBtn_10021": {
    "fields": {
        "clicked": false,
        "enable": true,
        "text": "LANJUTKAN KE PEMBAYARAN"
    },
    "id": "10021",
    "tag": "toPayBtn",
    "type": "biz"
}

这就是我的尝试:

jq '.toPayBtn*'

但结果是:

jq: error: syntax error, unexpected $end (Unix shell quoting issues?) at <top-level>, line 1:
.toPayBtn*         
jq: 1 compile error
exit status 3

正则表达式似乎不能与jq一起使用我如何修复它?

2 个答案:

答案 0 :(得分:1)

with_entries( select(.key | test("^toPayBtn_")) )

生成您想要的输出。您可能想要调整正则表达式。

答案 1 :(得分:0)

这是搜索框中的角度过滤器示例搜索文本,它只显示具有搜索关键字的对象。

<html ng-app="app">
<head>
    <title>App</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
    </script>


    <style>
        th {
            display: table-cell;
            vertical-align: inherit;
            font-weight: bold;
            text-align: center;
        }
    </style>
</head>

<body>
    <div ng-controller="ctrl">
       <input type="text" ng-model="search" placeholder="Search">
       {{search}}
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>OBJECT</th>
           
                </tr>
            </thead>
            <h4>{{displayById}}</h4>
            <tbody>
                <tr ng-repeat="x in jsonData | filter: search">
                    <td>{{x}}</td>
                </tr>
            </tbody>
        </table>

    </div>
    <script>
        var ngmodule = angular.module("app", []);

        ngmodule.controller("ctrl", ["$scope", "$log","$timeout",
            function ($scope, $log, $timeout) {

                $scope.init = function(){
                    console.log("$timeout")
                }
                $timeout($scope.init);

                $scope.jsonData = [{
    "orderTotal_1": {
        "fields": {
            "button": {
                "actionDialog": null,
                "actionUrl": null,
                "clicked": false,
                "enable": true,
                "text": "LANJUTKAN KE PEMBAYARAN",
                "textColor": null
            },
            "payment": {
                "pay": "Rp1.953.800",
                "taxTip": "Termasuk PPN, jika berlaku.",
                "title": "Total"
            },
            "timestamp": 1522355946093
        },
        "id": "1",
        "tag": "orderTotal",
        "type": "biz"
        },
    "rightContainer_10010": {
        "fields": {
            "css": {
                "backgroundColor": null,
                "floatPosition": "right",
                "marginTop": null,
                "width": "388px"
            }
        },
        "id": "10010",
        "tag": "rightContainer",
        "type": "container"
    },
    "toPayBtn_10021": {
        "fields": {
            "clicked": false,
            "enable": true,
            "text": "LANJUTKAN KE PEMBAYARAN"
        },
        "id": "10021",
        "tag": "toPayBtn",
        "type": "biz"
    },
    "voucherInput_1": {
        "fields": {
            "buttonText": "GUNAKAN",
            "placeHolder": "Masukkan Kode Voucher",
            "status": "default"
        },
        "id": "1",
        "tag": "voucherInput",
        "type": "biz"
    }
}];
        }]);
    </script>
</body>

</html>