Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JQ: Filter json array as per stedolan JQ library

I am quite new with JQ library.
This is the incoming response:

{"message":"Details fetched successfully","status":1,"details":[{"test":"a","amount":2500.0,"pre":["11","429393","543216","987657"]},{"test":"b","amount":1500.0,"pre":["480855"]},{"test":"c","amount":0.0,"pre":["No data present"]},{"test":"d","amount":1500.0,"pre":["526702"]},{"test":"e","amount":2500.0,"pre":["No data present"]}]}

Output required after applying JQ filter:

  {
  "msg": "Details fetched successfully",
  "status": 1,
  "details": {
      "a": {
        "amount": 2500,
        "pre": [
          "11",
          "429393",
          "543216",
          "987657"
        ]
      },
      "b": {
        "amount": 1500,
        "pre": [
          "480855"
        ]
      },
      "c": {
        "amount": 0,
        "pre": [
          "No data present"
        ]
      },
      "d": {
        "amount": 1500,
        "pre": [
          "526702"
        ]
      },
      "e": {
        "amount": 2500,
        "pre": [
          "No data present"
        ]
      }
  }
}

I tried using the below JQ filter but I am getting "details" as array but "details" is object in final output.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

JQ filter:

{msg: .message, status: .status, details: [.details[]| { (.test) : {amount: .amount, pre: .pre }}]}

>Solution :

INDEX can turn an array into an object with keys based on a sub-expression:

jq '.details |= INDEX(.test)' 
{
  "message": "Details fetched successfully",
  "status": 1,
  "details": {
    "a": {
      "test": "a",
      "amount": 2500,
      "pre": [
        "11",
        "429393",
        "543216",
        "987657"
      ]
    },
    "b": {
      "test": "b",
      "amount": 1500,
      "pre": [
        "480855"
      ]
    },
    "c": {
      "test": "c",
      "amount": 0,
      "pre": [
        "No data present"
      ]
    },
    "d": {
      "test": "d",
      "amount": 1500,
      "pre": [
        "526702"
      ]
    },
    "e": {
      "test": "e",
      "amount": 2500,
      "pre": [
        "No data present"
      ]
    }
  }
}

Demo


To delete the test field from the result, use map_values with del on the resulting object:

jq '.details |= (INDEX(.test) | map_values(del(.test)))'
{
  "message": "Details fetched successfully",
  "status": 1,
  "details": {
    "a": {
      "amount": 2500,
      "pre": [
        "11",
        "429393",
        "543216",
        "987657"
      ]
    },
    "b": {
      "amount": 1500,
      "pre": [
        "480855"
      ]
    },
    "c": {
      "amount": 0,
      "pre": [
        "No data present"
      ]
    },
    "d": {
      "amount": 1500,
      "pre": [
        "526702"
      ]
    },
    "e": {
      "amount": 2500,
      "pre": [
        "No data present"
      ]
    }
  }
}

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading