尝试从json提取数据时jq挂起

时间:2018-07-05 21:37:50

标签: jq

我的Json文件具有以下结构

{
"group0": [
    {
        "id": 9,
        "href": "href1",
        "name": "Americas",
        "group1": [
            {
                "id": 8,
                "href": "href2",
                "name": "Anguilla (UK)",
                "type": "country",
                "code": "ai",
                "group2": {
                    "field1": 9,
                    "field2": 1,
                    "field3": 1
                }
            },
            {
                "id": 9,
                "href": "href3",
                "name": "Antigua and Barbuda",
                "type": "country",
                "code": "ag",
                "group2": {
                    "field1": 9,
                    "field2": 1,
                    "field3": 2
                }
            },
            {
                "id": 10,
                "href": "href4",
                "name": "Argentina",
                "type": "country",
                "code": "ar",
                "group2": {
                    "field1": 8,
                    "field2": 1,
                    "field3": 2
                },
                "group3": [
                    {
                        "id": 599,
                        "href": "",
                        "name": "Rosario and Santa Fe (both Santa Fe province)",
                        "field3": 3
                    }
                ]
            }

        ]
    },
    {
        "id": 10,
        "href": "href5",
        "name": "Asia & the Pacific",
        "group1": [
            {
                "id": 1,
                "href": "href6",
                "name": "Afghanistan",
                "type": "country",
                "code": "af",
                "group2": {
                    "field1": 10,
                    "field2": 3,
                    "field3": 5
                }
            },
            {
                "id": 14,
                "href": "href7",
                "name": "Australia",
                "type": "country",
                "code": "au",
                "group2": {
                    "field1": 7,
                    "field2": 1,
                    "field3": 2
                }
            },
            {
                "id": 19,
                "href": "href8",
                "name": "Bangladesh",
                "type": "country",
                "code": "bd",
                "group2": {
                    "field1": 9,
                    "field2": 2,
                    "field3": 3
                },
                "group3": [
                    {
                        "id": 216,
                        "href": "",
                        "name": "Chittagong Hill Tracts; Dhaka",
                        "field3": 4
                    }
                ]
            }

        ]
    }  
]

}

我要提取文件group1.field0,group1.name,group2.field1,group1.field3

我正在运行的命令在下面

jq '.group0[].group1[]|"\(.name)|\(.field0)|\(.group2.field1)|\(.group2.field3)"' sample.json

当我在Solaris中运行以上命令时,它只是挂起而没有执行任何操作。当我运行以下命令时,我正在获取数据

jq '.group0[].group1[]|"\(.name)|\(.field0)"' sample.json

有人可以帮我做错什么吗?第一个命令正在jqplay.org上运行,它使用jq版本1.5,而我们服务器上的jq版本是1.4。我不知道是否可能是较低版本的问题(Solaris的最新版本仅为1.4)。

1 个答案:

答案 0 :(得分:0)

我已验证在MacOS上使用jq 1.4,jq 1.5或“开发”版本(主版)时不会出现此问题。

尽管如此,我认为值得升级到jq 1.5或更高版本。

以下内容可能会很好地工作:替换 (.group2.field1)的{​​{1}},(.group2|.field1)的类似情况