Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 527

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

$
0
0

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

Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>