I’m working with App Scripts to pull a JSON file from a GCS bucket. The data is stored as a newline delimited JSON. It arrives as a large object, which makes programmatic access to the internal data impossible.
My aim is to separate each element by merchant_id. I’ve attempted to split the data using \n as the parameter. But I’m having difficulty with converting the object to a string and back again.
Calling JSON.parse on the response gives me the error Unexpected token { in JSON at position 1766. While this does separate each of the elements within the JSON, calling JSON.parse on each individually gives me an Unexpected end of JSON input error.
Function
function myFunction() {
let url = 'url_address';
let storedData = [];
let response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
let str = response.toString();
let storedResponse = str.split("\n");
storedResponse.forEach((data) => {
storedData.push(JSON.parse(data));
});
}
Example of data in the forEach
{"product_data_timestamp":"2021-05-06 UTC","product_id":"product_id_here","merchant_id":"merchant_id_here","aggregator_id":"agg_id_here","offer_id":"offer_id_here","title":"furniture","description":"description","link":"link","additional_image_links":[],"content_language":"en","target_country":"GB","channel":"online","google_expiration_date":"2021-05-31 22:00:27.517 UTC","availability":"in stock","brand":"brand","color":"black","condition":"new","custom_labels":{"label_0":"GBP"},"item_group_id":"id","mpn":"mpn_id","price":{"value":"129.99","currency":"GBP"},"google_product_category_ids":[],"product_type":"headboards","additional_product_types":[],"destinations":[{"name":"Shopping","status":"approved"}],"issues":[]}