AndAsh AndAsh
AndAsh AndAsh

Reputation: 9

How to find and merge two json objects in a bash script using jq

I need to merge two json objects by key value in a bash script:

It is necessary to find by key (MAC address) the corresponding object (value) and add to its pairs the key: value pair of another object.

{"mac": "14:88:00:00:00:50", "key1": "value1", "key2": "value2"}
и
{
  "interfaces":{
    "m-11s-0": {
      "peers": {
        "14:88:00:00:00:13":{
          "next_hop":"14:88:00:00:00:85",
          "hop_count":"3",
          "path_change_count":"43",
          "metric":"357"
        },
        "14:88:00:00:00:50":{
          "next_hop":"14:88:00:00:00:85",
          "hop_count":"2",
          "path_change_count":"13",
          "metric":"163"
        },
        "14:88:00:00:00:85":{
          "next_hop":"14:88:00:00:00:85",
          "hop_count":"1",
          "path_change_count":"1",
          "metric":"48"
        }
      }
    }
  }
}

Found the right object:

logstatus_JSON=$(cat logstatus.sample)

echo "$logstatus_JSON" | jq -c '.interfaces["m-11s-0"].peers' | jq -r 'to_entries[] | if "\(.key)" == "14:88:00:00:00:50" then "\(.value)" else "zero" end'

There is very little information in the jq documentation. Please help me figure this out.

But I don't know how to combine two objects to get this result:

{
  "interfaces":{
    "m-11s-0": {
      "peers": {
        "14:88:00:00:00:13":{
          "next_hop":"14:88:00:00:00:85",
          "hop_count":"3",
          "path_change_count":"43",
          "metric":"357"
        },
        "14:88:00:00:00:50":{
          "next_hop":"14:88:00:00:00:85",
          "hop_count":"2",
          "path_change_count":"13",
          "metric":"163",
          "key1": "value1",
          "key2": "value2"
        },
        "14:88:00:00:00:85":{
          "next_hop":"14:88:00:00:00:85",
          "hop_count":"1",
          "path_change_count":"1",
          "metric":"48"
        }
      }
    }
  }
}

Upvotes: 0

Views: 81

Answers (1)

Mikhail Amk
Mikhail Amk

Reputation: 21

Based on the fact that you have used the Russian letter "и", it seems that you have two separate codes. Therefore, I have attempted to reproduce your issue in this way.

echo '{
  "interfaces":{
    "m-11s-0": {
      "peers": {
        "14:88:00:00:00:13":{
          "next_hop":"14:88:00:00:00:85",
          "hop_count":"3",
          "path_change_count":"43",
          "metric":"357"
        },
        "14:88:00:00:00:50":{
          "next_hop":"14:88:00:00:00:85",
          "hop_count":"2",
          "path_change_count":"13",
          "metric":"163"
        },
        "14:88:00:00:00:85":{
          "next_hop":"14:88:00:00:00:85",
          "hop_count":"1",
          "path_change_count":"1",
          "metric":"48"
        }
      }
    }
  }
}' > ~/tmp/test.txt

echo '{ "mac": "14:88:00:00:00:50", "key1": "value1", "key2": "value2" }'  >  ~/tmp/test1.txt

mac=$(jq -r '.mac'  ~/tmp/test1.txt)
for (( n=0; n<=1; n++ )); do
    key=$(jq keys[$n]  ~/tmp/test1.txt)
    value=$(jq -r '.'"${key}"'' ~/tmp/test1.txt)
    cat <<< $(jq --arg name ${value} '.interfaces["m-11s-0"].peers."'"$mac"'" .'"${key}"' = $name' ~/tmp/test.txt) > ~/tmp/test.txt
done
cat ~/tmp/test.txt

And I get what you wanted.

Upvotes: 0

Related Questions