Cách lấy value trong JSON

Mình có file JSON theo cấu trúc sau. Các bạn cho Mình hỏi là mình chỉ muốn lấy các giá trị Id, Image, Title thì sao sao ah…??

[{
  "categories" : {
    "2be21d23-ae6d-4a57-b4aa-2cee130c7fab" : {
      "Id" : "2be21d23-ae6d-4a57-b4aa-2cee130c7fab",
      "Image" : "woman_01-s5-large-578.jpg",
      "Title" : "ACCESORIES"
    },
    "5669d0b2-8d90-4101-a60e-e8ff4998d9df" : {
      "Id" : "5669d0b2-8d90-4101-a60e-e8ff4998d9df",
      "Image" : "story-winterwalk-imgsunglassesmoduler-large_1.jpg",
      "Title" : "WOMEN"
    },
    "c2a9fb38-12ee-4b60-bb6f-3faa1cb1e62b" : {
      "Id" : "c2a9fb38-12ee-4b60-bb6f-3faa1cb1e62b",
      "Image" : "james_bond_quote_1600x1200_1.jpg",
      "Title" : "MEN"
    },
    "e929051f-dfb6-4558-9ff9-26564a05b164" : {
      "Id" : "e929051f-dfb6-4558-9ff9-26564a05b164",
      "Image" : "child-childrens-baby-children-s_shcevh.jpg",
      "Title" : "KIDS"
    }
  }
}]

Cậu có thể tách bằng jq:

echo '[{
  "categories" : {
    "2be21d23-ae6d-4a57-b4aa-2cee130c7fab" : {
      "Id" : "2be21d23-ae6d-4a57-b4aa-2cee130c7fab",
      "Image" : "woman_01-s5-large-578.jpg",
      "Title" : "ACCESORIES"
    },
    "5669d0b2-8d90-4101-a60e-e8ff4998d9df" : {
      "Id" : "5669d0b2-8d90-4101-a60e-e8ff4998d9df",
      "Image" : "story-winterwalk-imgsunglassesmoduler-large_1.jpg",
      "Title" : "WOMEN"
    },
    "c2a9fb38-12ee-4b60-bb6f-3faa1cb1e62b" : {
      "Id" : "c2a9fb38-12ee-4b60-bb6f-3faa1cb1e62b",
      "Image" : "james_bond_quote_1600x1200_1.jpg",
      "Title" : "MEN"
    },
    "e929051f-dfb6-4558-9ff9-26564a05b164" : {
      "Id" : "e929051f-dfb6-4558-9ff9-26564a05b164",
      "Image" : "child-childrens-baby-children-s_shcevh.jpg",
      "Title" : "KIDS"
    }
  }
}]' | jq '.[] | .categories | map(values) | .[] | .Id,.Image,.Title'

Output:

"2be21d23-ae6d-4a57-b4aa-2cee130c7fab"
"woman_01-s5-large-578.jpg"
"ACCESORIES"
"5669d0b2-8d90-4101-a60e-e8ff4998d9df"
"story-winterwalk-imgsunglassesmoduler-large_1.jpg"
"WOMEN"
"c2a9fb38-12ee-4b60-bb6f-3faa1cb1e62b"
"james_bond_quote_1600x1200_1.jpg"
"MEN"
"e929051f-dfb6-4558-9ff9-26564a05b164"
"child-childrens-baby-children-s_shcevh.jpg"
"KIDS"
4 Likes

Ok, cám ơn bạn đã support

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?