Cara parsing file JSON dengan jq

File json banyak ditemui dalam kegiatan sehari-hari baik programmer, system/network engineer, dll.

File Json mempunyai struktur, pada tutorial ini hanya akan membahas bagaimana memparsing/mengurai file Json, misalnya hanya mengambil bagian data-data yang dibutuhkan saja. 

Disini menggunakan command jq, sebelum menggunakan perintah ini bisa menginstall terlebih dahulu, disini menggunakan linux, #sudo apt install jq

Gambar dibawah ini bagus memperlihatkan bagaimana format file json, isi file json pada dasarnya berisi data type pada umumnya.




Contoh ada file json dibawah ini:

score.json


{

  "eventname": "Lomba Siswa",

  "modulename": "Linux Project",

  "competitors": [

    {

      "username": "competitor1",

      "score": "98",

      "institution": "SMK Negeri 4"

    },

    {

      "username": "competitor2",

      "score": "78",

      "institution": "SMK Swasta 1"

    },

    {

      "username": "competitor3",

      "score": "70",

      "institution": "SMK Negeri 11"

    },

    {

      "username": "competitor4",

      "score": "71",

      "institution": "SMK Negeri 10"

    },

    {

      "username": "competitor5",

      "score": "87",

      "institution": "SMK Negeri 4"

    },

    {

      "username": "competitor6",

      "score": "70",

      "institution": "SMK Negeri 8"

    },

    {

      "username": "competitor7",

      "score": "79",

      "institution": "SMK Swasta 5"

    },

    {

      "username": "competitor8",

      "score": "62",

      "institution": "SMK Negeri 2"

    },

    {

      "username": "competitor9",

      "score": "50",

      "institution": "SMK Swasta 1"

    }

  ]

}


Berikut contoh untuk menggunakan command jq


Usage: jq [options] <jq filter> [file...]



1. Case 1

Menampilkan semua isi file json

cat score.json | jq .


2. Case 2

Menampilan data didalam array competitors, cukup tulis key name nya competitors maka akan keluar value nya.

cat score.json | jq .competitors

results:

[

  {

    "username": "competitor1",

    "score": "98",

    "institution": "SMK Negeri 1"

  },

  {

    "username": "competitor2",

    "score": "78",

    "institution": "SMK Swasta 1"

  },

  {

    "username": "competitor3",

    "score": "70",

    "institution": "SMK Negeri 11"

  },

  {

    "username": "competitor4",

    "score": "71",

    "institution": "SMK Negeri 10"

  },

  {

    "username": "competitor5",

    "score": "87",

    "institution": "SMK Negeri 4"

  },

  {

    "username": "competitor6",

    "score": "70",

    "institution": "SMK Negeri 1"

  },

  {

    "username": "competitor7",

    "score": "79",

    "institution": "SMK Swasta 5"

  },

  {

    "username": "competitor8",

    "score": "62",

    "institution": "SMK Negeri 2"

  },

  {

    "username": "competitor9",

    "score": "50",

    "institution": "SMK Swasta 1"

  }

]


Case 3 Menghitung jumlah data

cat score.json | jq '.competitors | length'
9

Case 4 Menampilkan username aja

Disini jq akan mengakses file json kemudian mencari key array object competitors dan menfilter username untuk ditampilkan

cat score.json | jq '.competitors[].username'
"competitor1"
"competitor2"
"competitor3"
"competitor4"
"competitor5"
"competitor6"
"competitor7"
"competitor8"
"competitor9"












Komentar

Postingan populer dari blog ini

Pembahasan IT Network Systems Administration Module A DNS (Forward Zone, Reverse Zone, CNAME, MX, Split View)

Cara restart / stop windows service (services.msc) dengan bat / cmd

How to convert VMDK to OVA