1

I have two files:

aws.json:
{
  "access_key_id": "MYSECRETKEYID",
  "secret_access_key": "mysecretaccesskey"
}



model.json:
{
    "access_key_id": "FREDSACCESSKEY",
    "secret_access_key": "fredssecretaccesskey",
    "ntp_servers_string": "1.2.3.4, 5.6.7.8",
    "metrics_ip": null,
    "pagerduty_enabled": false,
    "blobstore_type": "s3",
    "s3_blobstore_options": {
        "endpoint": "https://s3.amazonaws.com",
        "bucket_name": "s3-mybucket",
        "access_key_id": "vault-supplied-key",
        "secret_access_key": "vault-supplied-key",
        "signature_version": "4",
        "region": "us-east-1"
    },
    "database_type": "external"
}

and I would like to use jq to update a couple of values in the s3_blobstore_options key to the end result is this:

result.json:

{
    "access_key_id": "FREDSACCESSKEY",
    "secret_access_key": "fredssecretaccesskey",
    "ntp_servers_string": "1.2.3.4, 5.6.7.8",
    "metrics_ip": null,
    "pagerduty_enabled": false,
    "blobstore_type": "s3",
    "s3_blobstore_options": {
        "endpoint": "https://s3.amazonaws.com",
        "bucket_name": "s3-mybucket",
        "access_key_id": "MYSECRETKEYID",
        "secret_access_key": "mysecretaccesskey",
        "signature_version": "4",
        "region": "us-east-1"
    },
    "database_type": "external"
}

The following code works perfectly, if the values are not nested:

jq --argfile override aws.json '. + $override' model.json > result.json

Anyone know how to tell jq that I want to update the lower-level keys pairs, and not touch the upper level ones (i.e. FREDSACCESSKEY)?

1 Answer 1

1

You could simply run:

jq --argfile aws aws.json '.s3_blobstore_options += $aws' model.json

Or if the jq filter is in aws.jq:

jq --argfile aws aws.json -f aws.jq model.json

The idea here is that when applying + to JSON objects, the values in the RHS object take precedence.

There are other ways for the jq program to access the contents of aws.json ...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! the first option works great! (not sure what aws.jq is, that fails on my machine)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.