'Change json object with jq

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"
    }
  ],


Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1