Skip to content

Instantly share code, notes, and snippets.

@ZReC
Created March 13, 2022 22:27
Show Gist options
  • Save ZReC/42ef581c5f72cda1b6b09da3e59120b7 to your computer and use it in GitHub Desktop.
Save ZReC/42ef581c5f72cda1b6b09da3e59120b7 to your computer and use it in GitHub Desktop.
Remove duplicate entries from an exported Bitwarden .json vault
<input type="file" accept=".json">
<a style="display: none;"></a>
<script>
/**
* The following code should remove duplicate items within a .json vault that was
* exported from Bitwarden v1.31.3 and maybe higher (needs more testing).
* Once the right .json is provided, it will prompt a dialog to save a non-formatted out.json
*
* Copyright 2022 ZReC & others
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
const file = document.querySelector('input');
file.addEventListener('change', e => {
for (const f of e.target.files) {
const read = new FileReader();
read.addEventListener('load', async e => {
const str = e.target.result;
const json = JSON.parse(str);
if ('items' in json) {
const hash = {};
for (const prop in json.items) {
const item = json.items[prop];
const id = item.id;
delete item.id;
const itemhash = new TextDecoder()
.decode(
await crypto.subtle.digest(
'sha-256',
new TextEncoder()
.encode(JSON.stringify(item))
)
);
if (!(itemhash in hash)) {
item.id = id;
hash[itemhash] = item;
}
}
const final = [];
for (const prop in hash) {
final.push(hash[prop]);
}
json.items = final;
const send = document.querySelector('a');
var blob = new Blob([JSON.stringify(json)], { type: 'text/plain' });
send.href = window.URL.createObjectURL(blob);
send.download = 'out.json';
send.dataset.downloadurl = ['text/plain', send.download, send.href].join(':');
send.click();
}
});
read.readAsText(f);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment