Fetching data from an API will result in various JSON objects with differing lengths and content, including a mix of objects and arrays that can be nested multiple levels deep.
The goal is to extract all data pairs and consolidate them into a single 2D array for easier integration into Google Sheets using a script.
This is the initial code:
function parseTheAPI(rawResponse){
var theparsedJSONdata = JSON.parse(rawResponse)
console.log(theparsedJSONdata)
return theparsedJSONdata
};
Below is the output seen on the console:
{ '0xabcxyz':
{ products: [ [Object] ],
meta: [ [Object], [Object], [Object] ] } }
Although individual data points can be accessed once the object contents are known, any changes in the object structure could break the code.
The objective is to dynamically access all information pairs within the JSON data at any depth.
Attempts have been made to use recursion to explore the depths of the data, as shown below:
function logJsonLevelOne(parsedJson){
for(var k in parsedJson){
if(parsedJson[k] instanceof Object){
console.log("parsedJsonOne = "+ parsedJson[k])
var n = Array.isArray(logJsonLevelOne[k])
logJsonLevelOne(parsedJson[k])
} else {
var n = logJsonLevelOne[k] instanceof Array;
console.log(n)
}
}
};
While some data is printed, accessing the next level remains unclear. Understanding the nature of the data coming out of each part of the "if" test proves challenging.
Efforts have been put into extracting this data into two rows on a Google Sheet for daily tracking, considering potential variations in the incoming JSON object structure.
Referencing raw data from the API:
{"0x0000123456abcdef":{...}}}
Further attempts were made using code from a Stack Overflow question, but certain outputs are not appearing as expected.