Home > Enterprise >  Change json object with jq
Change json object with jq

Time:03-16

how can I update servers object in my json using jq?

"tags": [],
  "servers": [],
  "components": {
    "securitySchemes": {
      "cookie": {
        "type": "apiKey",
        "in": "cookie",
        "name": "store",
        "description": "Store API"
      }
    },

I need to have servers like that:

      "servers": [
    {
      "url": "http://localhost:5000",
      "description": "Localhost server"
    }
  ],

CodePudding user response:

echo '{"tags": [],
  "servers": [],
  "components": {
    "securitySchemes": {
      "cookie": {
        "type": "apiKey",
        "in": "cookie",
        "name": "store",
        "description": "Store API"
      }
    }
}}' | jq '.servers = [{url:"http://localhost:5000", description: "Localhost server"}]'

Or, for a fixed json file (adding missing }):

jq '.servers = [{url:"http://localhost:5000", description: "Localhost server"}]' file.json

CodePudding user response:

I have something like that for now:

    - cat /zap/wrk/api.json | jq '.servers = [{url:"http://localhost:3000", description: "Localhost server"}]' api.json > "$tmp" && mv "$tmp" api.json

but it's not yml valid

  • Related