See EDIT below for new answer.
The stated output is not deterministic, a comma can be either part of a cell or a separator, and there is no way to know which one it is.
Instead of JSON, you can publish a sheet as a .csv as follows:
- select sheet to publish, such as
Sheet2
- select format
Comma-separated values (.csv)
After that you can access the CSV data with URL:
https://docs.google.com/spreadsheets/d/e/<spreadsheet-id>/pub?gid=<tab-id>&single=true&output=csv
Example sheet:
| A1 cell | B1 |
| A2, stuff | B2 |
Example returned CSV content:
A1 cell,B1
"A2, stuff",B2
There are plenty of tools to parse CSV, such as https://github.com/peterthoeny/parse-csv-js
EDIT, after learning that Esteban can't make changes to the data source:
You can use a split and forEach to construct the desired object:
const input = 'a: Feb 21, 10:11, b: some content, c: more, d: even more';
let result = {};
input.split(/, (?=[a-z]+:)/)
.forEach(item => {
let key = item.replace(/:.*/, '');
let val = item.replace(/.*?: */, '');
result[key] = val;
});
console.log(JSON.stringify(result, null, ' '));
Output:
{
"a": "Feb 21, 10:11",
"b": "some content",
"c": "more",
"d": "even more"
}
Explanation:
let result = {}; - initialize and empty result object
.split(/, (?=[a-z]+:)/) - split between key/value pairs using a positive lookahead for an alpha: key
.forEach():
- extract the key and value from the item
- add new property to result object, property name is key