Skip to content
Advertisement

Using Google Chrome extensions to import/export JSON files?

I’m creating a Google Chrome extension at the moment and I was wondering if it’s possible for it to both create JSON files to download (export) and create a button where users can make the extension open and parse JSON files that they have saved in their local file system or on a USB stick (import)?

The parsing of each JSON from the local file system would simply involve reading off each key-value pair and doing something with the data. It would only have to deal with strings, so nothing complicated.

**EDIT: **My question is not a duplicate of this one because I’m not interested in changing the user’s download path. All I want is to enable them to, with their consent, download a file to their normal download directory (which Filesaver.js can do). Plus, that post says nothing about importing.

Advertisement

Answer

You can fake link to “download” imaginary array MyData or whatever,:

JavaScript

this will export/download your array into json file named as vName variable.


If you wish to import/read file:
create input element (type=file) and make it invisible (here I’m having html element and then adding js listener in script)

JavaScript

script

JavaScript

make button fakeImp (or any element), that you can style as you wish and that will be used as trigger for importing event

JavaScript

import function (from listener)

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement