Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Parse nulls in JSON (Also includes a couple of other fixes found in issues) #149

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions ImportJSON.gs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*====================================================================================================================================*
ImportJSON by Brad Jasper and Trevor Lohrbeer
====================================================================================================================================
Version: 1.5.0
Version: 1.5.0 + parse nulls fix + parse stringified numbers as numeric + don't truncate numbers (and thus keep numeric)
Project Page: https://github.com/bradjasper/ImportJSON
Copyright: (c) 2017-2019 by Brad Jasper
(c) 2012-2017 by Trevor Lohrbeer
Expand Down Expand Up @@ -48,12 +48,13 @@
*
* To change this behavior, pass in one of these values in the options parameter:
*
* noInherit: Don't inherit values from parent elements
* noTruncate: Don't truncate values
* rawHeaders: Don't prettify headers
* noHeaders: Don't include headers, only the data
* allHeaders: Include all headers from the query parameter in the order they are listed
* debugLocation: Prepend each value with the row & column it belongs in
* noInherit: Don't inherit values from parent elements
* noTruncate: Don't truncate values
* rawHeaders: Don't prettify headers
* noHeaders: Don't include headers, only the data
* allHeaders: Include all headers from the query parameter in the order they are listed
* debugLocation: Prepend each value with the row & column it belongs in
* noParseNumbers: Don't parse numbers (keep as text)
*
* For example:
*
Expand Down Expand Up @@ -434,7 +435,7 @@ function transformData_(data, options, transformFunc) {
* Returns true if the given test value is an object; false otherwise.
*/
function isObject_(test) {
return Object.prototype.toString.call(test) === '[object Object]';
return typeof(test) == 'object' && test !== null;
}

/**
Expand Down Expand Up @@ -493,7 +494,7 @@ function applyXPathRule_(rule, path, options) {
* debugLocation: Prepend each value with the row & column it belongs in
*/
function defaultTransform_(data, row, column, options) {
if (data[row][column] == null) {
if (data[row][column] === undefined) {
if (row < 2 || hasOption_(options, "noInherit")) {
data[row][column] = "";
} else {
Expand All @@ -509,15 +510,29 @@ function defaultTransform_(data, row, column, options) {
data[row][column] = toTitleCase_(data[row][column].toString().replace(/[\/\_]/g, " "));
}

if (!hasOption_(options, "noTruncate") && data[row][column]) {
if (!hasOption_(options, "noParseNumbers") && typeof(data[row][column]) != 'number') {
var num = filterFloat(data[row][column]);
if (!isNaN(num)) {
data[row][column] = num;
}
}

if (!hasOption_(options, "noTruncate") && typeof(data[row][column]) != 'number' && data[row][column]) {
data[row][column] = data[row][column].toString().substr(0, 256);
}

if (hasOption_(options, "debugLocation")) {
data[row][column] = "[" + row + "," + column + "]" + data[row][column];
}
}

function filterFloat(value) {
if(/^(-|\+)?([0-9]+(.[0-9]+)?|Infinity)$/.test(value)) {
return Number(value);
}
return NaN;
}

/**
* If all the values in the given row share the same prefix, remove that prefix.
*/
Expand Down