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

How to exclude the elements from one array that exist in another array by some of their properties using jq?

$
0
0

Assuming two json files:a.json

[  {"category": "utils","name": "yq"  },  {"category": "utils","name": "yq4"  },  {"category": "utils","name": "yip"  },  {"category": "utils","name": "jq"  },  {"category": "utils","name": "yip-systemd"  },  {"category": "utils","name": "system-tools"  }]

b.json

[  {"category": "utils","name": "jq"  },  {"category": "utils","name": "yq"  },  {"category": "utils","name": "yip"  },  {"category": "utils","name": "yq4"  },  {"category": "utils","name": "yip-systemd"  },  {"category": "utils","name": "system-tools"  },  {"category": "utils","name": "test"  }]

In order to get the objects in b that are not in a, can run:

jq -n --argfile a a.json --argfile b b.json '$b-$a | .[] | .category +"/" + .name'

the result being:

"utils/test"

Using jq, how to manage the situation when additional properties are set, having different values"?

For example:a.json

[  {"category": "utils","name": "yq","version": "1"  },  {"category": "utils","name": "yq4","version": "1"  },  {"category": "utils","name": "yip","version": "1"  },  {"category": "utils","name": "jq","version": "1"  },  {"category": "utils","name": "yip-systemd","version": "1"  },  {"category": "utils","name": "system-tools","version": "1"  }]

b.json

[  {"category": "utils","name": "jq","version": "2"  },  {"category": "utils","name": "yq","version": "2"  },  {"category": "utils","name": "yip","version": "2"  },  {"category": "utils","name": "yq4","version": "2"  },  {"category": "utils","name": "yip-systemd","version": "2"  },  {"category": "utils","name": "system-tools","version": "2"  },  {"category": "utils","name": "test","version": "2"  }]

having the result:

"utils/test"

Viewing all articles
Browse latest Browse all 657

Trending Articles



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