Search code examples
jsonjqparameterization

How to parse this file with jq?


I just started using and json files, and I'm trying to parse a specific file. I'm tring to do it with jq in command line, but if there's any other way to do it properly, I'm in to give it a try.

The file itself looks like this :

{
  "Status": "ok",
  "Code": 200,
  "Message": "",
  "Result": [
    {
      "ID": 123456,
      "Activity": 27,
      "Name": Example1",
      "Coordinate": {
        "Galaxy": 1,
        "System": 22,
        "Position": 3
  },
      "Administrator": false,
      "Inactive": false,
      "Vacation": false,
      "HonorableTarget": false,
      "Debris": {
        "Metal": 0,
        "Crystal": 0,
        "RecyclersNeeded": 0
      },
      "Moon": null,
      "Player": {
        "ID": 111111,
        "Name": "foo",
        "Rank": 4
      },
      "Alliance": null
    },
    {
      "ID": 223344,
      "Activity": 17,
      "Name": "Example2",
      "Coordinate": {
            "Galaxy": 3,
            "System": 44,
            "Position": 5
          },
          "Administrator": false,
          "Inactive": false,
          "Vacation": false,
          "StrongPlayer": false,
          "HonorableTarget": false,
          "Debris": {
            "Metal": 0,
            "Crystal": 0,
            "RecyclersNeeded": 0
          },
          "Moon": null,
          "Player": {
            "ID": 765432,
            "Name": "Player 2",
            "Rank": 3
          },
          "Alliance": null
        },
  (...)
  ]
}

I would need to extract information based on the galaxy/system/position. For example, having a script with the proper filters in it and execute something like that :

./parser --galaxy=1 --system=22 --position=3

And it would give me :

ID : 123456
Name : Example1
Activity : 27
...

I tried to do that with curl to grab my json file and jq to parse my file, but I have no idea how I can make that kind of request.


Solution

  • The following should be sufficient to get you on your way.

    First, let's assume the JSON is in a file name galaxy.json; second, let's assume the file galaxy.jq contains the following:

    .Result[]
    | select(.Coordinate | (.Galaxy==$galaxy and .System==$system and .Position==$position))
    

    Then the invocation:

    jq -f so-galaxy.jq --argjson galaxy 1 --argjson system 22 --argjson position 3 galaxy.json
    

    would yield the corresponding object:

    {
      "ID": 123456,
      "Activity": 27,
      "Name": "Example1",
      "Coordinate": {
        "Galaxy": 1,
        "System": 22,
        "Position": 3
      },
      "Administrator": false,
      "Inactive": false,
      "Vacation": false,
      "HonorableTarget": false,
      "Debris": {
        "Metal": 0,
        "Crystal": 0,
        "RecyclersNeeded": 0
      },
      "Moon": null,
      "Player": {
        "ID": 111111,
        "Name": "foo",
        "Rank": 4
      },
      "Alliance": null
    }
    

    Key: Value format

    If you want the output to be in key: value format, simply add -r to the command-line options, and append the following to the jq filter:

    | to_entries[]
    | "\(.key): \(.value)"
    

    Output

    ID: 123456
    Activity: 27
    Name: Example1
    Coordinate: {"Galaxy":1,"System":22,"Position":3}
    Administrator: false
    Inactive: false
    Vacation: false
    HonorableTarget: false
    Debris: {"Metal":0,"Crystal":0,"RecyclersNeeded":0}
    Moon: null
    Player: {"ID":111111,"Name":"foo","Rank":4}
    Alliance: null