简体   繁体   中英

How to extract specific object from JSON with arrays via jq

How can I extract only the objects id and name from this JSON via jq?

The output should be look like the format below. This is just an example for the required output, the real one that I need is to catch the whole values id and name like in the JSON source.

This is the required output:

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

This is the JSON source file:

{
  "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"
        }
      ]
    } ] }

i think what you want is Recursive Descent: ..

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

to produce something like in your example,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM