如何通过jq从数组中提取特定对象

时间:2018-05-10 12:30:03

标签: arrays json jq

如何通过jq从此JSON中仅提取对象idname

输出应该类似于下面的格式。这只是所需输出的一个示例,我需要的实际值是捕获JSON源中的整个值idname

这是必需的输出:

{
 "name": "Auto Body Styles",
      "id": "1.1"}
{
          "name": "Convertible",
          "id": "1.1.2"
                 } 

这是JSON源文件:

{
  "name": "Automotive",
  "id": "1",
  "categories": [
    {
      "name": "Auto Body Styles",
      "id": "1.1",
      "categories": [
        {
          "name": "Commercial Trucks",
          "id": "1.1.1"
        },
        {
          "name": "Convertible",
          "id": "1.1.2"
        },
        {
          "name": "Coupe",
          "id": "1.1.3"
        },
        {
          "name": "Crossover",
          "id": "1.1.4"
        },
        {
          "name": "Hatchback",
          "id": "1.1.5"
        },
        {
          "name": "Microcar",
          "id": "1.1.6"
        },
        {
          "name": "Minivan",
          "id": "1.1.7"
        },
        {
          "name": "Off-Road Vehicles",
          "id": "1.1.8"
        },
        {
          "name": "Pickup Trucks",
          "id": "1.1.9"
        },
        {
          "name": "Sedan",
          "id": "1.1.10"
        },
        {
          "name": "Station Wagon",
          "id": "1.1.11"
        },
        {
          "name": "SUV",
          "id": "1.1.12"
        },
        {
          "name": "Van",
          "id": "1.1.13"
        }
      ]
    },
    {
      "name": "Auto Buying and Selling",
      "id": "1.2"
    },
    {
      "name": "Auto Insurance",
      "id": "1.3"
    },
    {
      "name": "Auto Parts",
      "id": "1.4"
    },
    {
      "name": "Auto Recalls",
      "id": "1.5"
    },
    {
      "name": "Auto Repair",
      "id": "1.6"
    },
    {
      "name": "Auto Safety",
      "id": "1.7"
    },
    {
      "name": "Auto Shows",
      "id": "1.8"
    },
    {
      "name": "Auto Technology",
      "id": "1.9",
      "categories": [
        {
          "name": "Auto Infotainment Technologies",
          "id": "1.9.1"
        },
        {
          "name": "Auto Navigation Systems",
          "id": "1.9.2"
        },
        {
          "name": "Auto Safety Technologies",
          "id": "1.9.3"
        }
      ]
    },
    {
      "name": "Auto Type",
      "id": "1.10",
      "categories": [
        {
          "name": "Budget Cars",
          "id": "1.10.1"
        },
        {
          "name": "Certified Pre-Owned Cars",
          "id": "1.10.2"
        },
        {
          "name": "Classic Cars",
          "id": "1.10.3"
        },
        {
          "name": "Concept Cars",
          "id": "1.10.4"
        },
        {
          "name": "Driverless Cars",
          "id": "1.10.5"
        },
        {
          "name": "Green Vehicles",
          "id": "1.10.6"
        }
      ]
    } ] }

1 个答案:

答案 0 :(得分:1)

我认为你想要的是Recursive Descent: ..

cat car.json | jq -r '.. | [.name?, .id?] | select(length>0) | @tsv'

生成类似于你的例子的内容,

cat car.json | jq -r '.. | {name:.name?, id:.id?}'