Home > front end >  Integrate json values into another file
Integrate json values into another file

Time:05-20

I'm trying to update an existing json file from values in another json file using jq in a bash shell.

I've got a settings json file

{
  "Logging": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": "Warning"
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "./logs/log-.txt",
          "rollingInterval": "Day"
        }
      }
    ]
  },
  "Settings": {
    "DataServerUrl": "https://address.to.server.com",
    "ServerKey": "1f969476798adfe95114dd28ed3a3ff"
    "ServerTimeZone": "Mountain Standard Time",
    "MaxOccupantCount": 6
  }
}

In an integration step, I'm attempting to incorporate values for specific environments (think dev/staging/prod) from an external json file with limited setting values. An example of such a file is

{
    "DataServerUrl": "https://dev.server.addr.com",
    "ServerKey": "2a4d99233efea456b95114aa23ed342ae"
}

I can get to the data using jq. I can update the data using jq if I hard-code the updates. I'm looking for something general to take in any environment settings values and update them in the base settings file. My searches suggest I can do this in a single step without knowing the specific values. A command similar to

jq -r 'to_entries[]' settings.dev.json |
  while IFS= read -r key value; do
    jq -r '.[$key] |= [$value]' settings.json
  done

What happens is I get error messages stating jq: error: $key is not defined at <top-level> (as well as the same message for $value). The messages appear several times in pairs. settings.json is not changed. Now, this makes partial sense because the output from just jq -r 'to_entries[]' settings.dev.json looks like (empty space in this output is included as produced by the command).

 
  "key": "DataServerUrl",
  "value": "https://dev.server.addr.com"


  "key": "ServerKey",
  "value": "2a4d99233efea456b95114aa23ed342ae"


How do I go about iterating over the values in the environment settings file such that I can use those values to update the base settings file for further processing (i.e., publishing to the target environment)?

CodePudding user response:

You could do something like

jq -s '.[1] as $insert | .[0].Settings |= $insert | .[0]' settings.json insert.json

Where we :

  • slurp both files
  • Save insert.json to a variable called $insert
  • Append (|=) $insert to .[0].Settings
  • Show only the first file .[0]

So the output will become:

{
  "Logging": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": "Warning"
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "./logs/log-.txt",
          "rollingInterval": "Day"
        }
      }
    ]
  },
  "Settings": {
    "DataServerUrl": "https://dev.server.addr.com",
    "ServerKey": "2a4d99233efea456b95114aa23ed342ae"
  }
}

CodePudding user response:

The simplest way is to provide both files and address the second one using input. That way, all you need is the assignment:

jq '.Settings = input' settings.json insert.json
{
  "Logging": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": "Warning"
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "./logs/log-.txt",
          "rollingInterval": "Day"
        }
      }
    ]
  },
  "Settings": {
    "DataServerUrl": "https://dev.server.addr.com",
    "ServerKey": "2a4d99233efea456b95114aa23ed342ae"
  }
}

Demo

  • Related