json2gsheet
v2.0.2
Published
Serializes JSON data to Google Sheet, and vice versa
Downloads
9
Readme
json2gsheet
Serializes JSON data to Google Sheets, and vice versa.
Installation
npm install json2gsheet
Concept
json2gsheet
pushes JSON keys to a column, and values to another column, and more values for the same key to subsequent columns.
json2gsheet
uses a token, which is called an id
in this context, to relate the JSON file and the sheet column where values are pushed to.
For example, given these JSON files, with id
as the file name without the .json
suffix:
// person1.json
{
"name": "John",
"likes": "puppy"
}
// person2.json
{
"name": "Jane",
"likes": "cat"
}
// person3.json
{
"name": "Russell",
"likes": "bear"
}
When pushed to the sheet, this is the result:
Key | person1 | person2 | person3
-----------------------------------
name | John | Jane | Russell
likes | puppy | cat | bear
For nested JSON object, it is first flattened when pushed to the sheet. For example:
// someCol.json
{
"parent": {
"child": "some value",
"childtwo": {
"grandchild": "more value"
}
}
}
becomes:
Key | someCol
---------------------------------------
parent.child | some value
parent.childtwo.grandchild | more value
When pulled from the sheet, it is de-flattened to restore the initial nested structure.
Scope
json2gsheet
only works with JSON strings, objects, and arrays.
Usage
Preparation
In a working directory, prepare these files:
json2gsheet.config.json
Configuration file for this application.
client_secret.json
Google API credential in JSON format.
To get your client_secret.json
:
- On a Google Cloud Platform project, enable Google Sheets API.
- Create a service account, note its email address.
- Download the JSON credential file and name it as
client_secret.json
.
On your sheet, grant Editor access to the service account, via its email address.
Pushing JSON to sheet
json2gsheet push <id>
What it does:
- Read the JSON file identified by
id
- Flatten it to have a single level key-value pairs
- Push the list of keys and values to their respective sheet column as specified in the configuration file
Pulling from sheet to JSON
json2gsheet pull <id>
What it does:
- Pull data from the sheet
- De-flatten the data
- Write the JSON to a file identified by
id
Basically the opposite of push
subcommand.
Configuration
json2gsheet
is heavily driven by configurations. You can find a copy of sample configuration in this repository.
App configurations
app.jsonFileName
The file name template for the JSON file. This is where the position of
id
token is specified, using the placeholder$id
.app.command.pull.skipEmptyValue
For
pull
subcommand only. If set totrue
, when a cell is empty, the key-value pair represented by this cell will not be inserted in the resulting JSON object.
Sheets configurations
sheets.spreadsheetId
The Google Sheets ID.
sheets.sheetName
The name of the sheet to read from or write to. Note, this is not the spreadsheet's file name, but the name of an individual sheet in the spreadsheet file.
sheets.keyColumn
The column to push JSON keys to. It is an object containing:
label
for column header labelcolumn
to push tocellStart
on which cell to start writing from
sheets.valueColumns
The columns to push JSON values to.
This is an array of
valueColumn
. Each object contains:id
to identify the JSON filelabel
for column header labelcolumn
to push tocellStart
on which cell to start writing from
Development
Run the tests:
npm test
To prepare for a new version:
- Create a new branch.
- Update version on
package.json
andpackage-lock.json
. - Make a commit.
- Merge the branch.
- Create an annotated tag.
- Push the tag.
To publish the new version:
npm pack
npm publish