Serializes JSON data to Google Sheets, and vice versa
Serializes JSON data to Google Sheets, and vice versa.
npm install json2gsheet
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.
json2gsheet
only works with JSON strings, objects, and arrays.
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
:
client_secret.json
.On your sheet, grant Editor access to the service account, via its email address.
json2gsheet push <id>
What it does:
id
json2gsheet pull <id>
What it does:
id
Basically the opposite of push
subcommand.
json2gsheet
is heavily driven by configurations. You can find a copy of sample configuration in this repository.
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 to true
, when a cell is empty, the key-value pair represented by this cell will not be inserted in the resulting JSON object.
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 fromsheets.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 fromRun the tests:
npm test
To prepare for a new version:
package.json
and package-lock.json
.To publish the new version:
npm pack
npm publish