Vageesh S M
Vageesh S M

Reputation: 21

how to remove square brackets from particular place in json file in linux using jq

i want to remove square brackets as illustrated in this example:

Input file : file1.json

{ "regex_features": [
      [ // This bracket needs to be removed
        {
          "name": "jobname",
          "pattern": "[A-Z]+-[0-9]"
        },
        {
          "name": "project",
          "pattern": "[A-Z]{4}"
        },
        {
          "name": "summary",
          "pattern": "[a-z]+[a-z]+[a-z]{4}"
        },
        {
          "name": "description",
          "pattern": "[a-z]+[a-z]+[a-z]+[a-z]{4}"
        }
      ] // this bracket needs to be removed. 
     ]
}

please suggest.

Upvotes: 0

Views: 3948

Answers (1)

hek2mgl
hek2mgl

Reputation: 158110

Let's say you have this file.json:

{
  "name": "foo",
  "regex_features": [
    [
      {
        "name": "jobname",
        "pattern": "[A-Z]+-[0-9]"
      },
      {
        "name": "project",
        "pattern": "[A-Z]{4}"
      }
    ]
  ]
}

You can use the update assignment operator to replace the list by its first element (which is the inner list):

jq '.regex_features|=.[0]' file.json

Output:

{
  "name": "foo",
  "regex_features": [
    {
      "name": "jobname",
      "pattern": "[A-Z]+-[0-9]"
    },
    {
      "name": "project",
      "pattern": "[A-Z]{4}"
    }
  ]
}

Upvotes: 2

Related Questions