Passing JSON variable to jq for manipulation

Viewed 27

i have a variable with json value and need to add that value in specific json field

$ echo $x | jq
{
  "displayName": "Cloud Pub/Sub Subscription - Oldest unacked message age",
  "conditionThreshold": {
    "aggregations": [
      {
        "alignmentPeriod": "60s",
        "crossSeriesReducer": "REDUCE_MEAN",
        "groupByFields": [
          "resource.label.project_id",
          "resource.label.subscription_id"
        ],
        "perSeriesAligner": "ALIGN_MEAN"
      }
    ],
    "comparison": "COMPARISON_GT",
    "duration": "0s",
    "filter": "resource.type = \"pubsub_subscription\" AND (resource.label.\"subscription_id\"=\"chanda_test_queue \" AND resource.labels.project_id = \"test_project\") AND metric.type = \"pubsub.googleapis.com/subscription/oldest_unacked_message_age\"",
    "thresholdValue": 1000000,
    "trigger": {
      "count": 1
    }
  }
}

i want to add this value in other json in specific filed:


{
  "displayName": "pubsub-inbound-queue-unack-message-age",
  "userLabels": {
    "policy_name": "pubsub-inbound-queue-unack-message-age",
    "policy_version": "001",
    "project_name": "@@project_name@@",
    "created_by": "rubik"
  },
  "conditions": [
    #here #
  ],
  "alertStrategy": {
    "autoClose": "604800s"
  },
  "combiner": "OR",
  "enabled": true,
  "notificationChannels": []
}

i am doing this by jq --arg test "$x" '.["conditions"][0] |= .+ {$test}'

command but getting \n in multiple positions . need some suggestions here

cat custom-pubsub-alerting.json.template | jq --arg test "$x" '.["conditions"][0] |= .+ {$test}'

output:

{
  "displayName": "pubsub-inbound-queue-unack-message-age",
  "userLabels": {
    "policy_name": "pubsub-inbound-queue-unack-message-age",
    "policy_version": "001",
    "project_name": "@@project_name@@",
    "created_by": "rubik"
  },
  "conditions": [
    {
      "test": "{\n  \"displayName\": \"Cloud Pub/Sub Subscription - Oldest unacked message age\",\n  \"conditionThreshold\": {\n    \"aggregations\": [\n      {\n        \"alignmentPeriod\": \"60s\",\n        \"crossSeriesReducer\": \"REDUCE_MEAN\",\n        \"groupByFields\": [\n          \"resource.label.project_id\",\n          \"resource.label.subscription_id\"\n        ],\n        \"perSeriesAligner\": \"ALIGN_MEAN\"\n      }\n    ],\n    \"comparison\": \"COMPARISON_GT\",\n    \"duration\": \"0s\",\n    \"filter\": \"resource.type = \\\"pubsub_subscription\\\" AND (resource.label.\\\"subscription_id\\\"=\\\"chanda_test_queue \\\"  AND resource.labels.project_id = \\\"test_project\\\") AND metric.type = \\\"pubsub.googleapis.com/subscription/oldest_unacked_message_age\\\"\",\n    \"thresholdValue\": 1000000,\n    \"trigger\": {\n      \"count\": 1\n    }\n  }\n}"
    }
  ],
  "alertStrategy": {
    "autoClose": "604800s"
  },
  "combiner": "OR",
  "enabled": true,
  "notificationChannels": []
}

0 Answers
Related