Search code examples
jsonbashshjq

jq array filtration from a file to another file


I have the below json in which i want to filter out the result object which is an Array using some script, since result object can populate multiple objects in it. I wanted to get the data from "jq -c .results[i]."com.src.main.client.details" into a json file to process further.

{
    "foo": {
        "requestID": "89279f54-2f18-4301-b94d-1c413be1cb68",
        "signature": {
            "*": "*"
        }
    },
    "results": [
        {
            "com.src.main.client.details": {
                "doc": "string",
                "details": [
                    {
                        "amount": null,
                        "output": null,
                        "properties": [],
                        "characteristic": [],
                        "component": null,
                        "period": null,
                        "internals": {
                            "Currency": "EUR",
                           "value": 0
                        }
                    }
                ]
            }
        },
        {
            "com.src.main.client.details": {
                "doc": "string",
                "details": [
                    {
                        "amount": null,
                        "output": null,
                        "properties": [
                            {
                                "characteristic": [],
                                "component": null,
                                "period": null,
                                "internals": {
                                    "Currency": "EUR",
                                    "value": 0
                                }
                            }
                        ]
                    }
                ]
            }
        }
    ]
}

Is there a way I can achieve it via one single command or if someone can suggest the scripting logic. Thanks.

Desire Output:

[
  {
    "doc": "string",
    "details": [
      {
        "amount": null,
        "output": null,
        "properties": [],
        "characteristic": [],
        "component": null,
        "period": null,
        "internals": {
          "Currency": "EUR",
          "value": 0
        }
      }
    ]
  },
  {
    "doc": "string",
    "details": [
      {
        "amount": null,
        "output": null,
        "properties": [
          {
            "characteristic": [],
            "component": null,
            "period": null,
            "internals": {
              "Currency": "EUR",
              "value": 0
            }
          }
        ]
      }
    ]
  }
]

Solution

  • Three issues.


    .results[i] should be .results[]


    The following produces a stream of JSON objects:

    .results[]."com.src.main.client.details"
    

    To get an array, use

    [ .results[]."com.src.main.client.details" ]
    

    or

    .results | map(."com.src.main.client.details")
    

    Finally, there was a shell quoting issue. In a "sh-like" shell, you want

    jq -c '.results | map(."com.src.main.client.details")'
    

    Note the single quotes.


    Demo on jqplay