I am pulling data from an API that is going to return JSON objects of varying lengths and content. It is a mishmash of objects and arrays that can be several levels deep.
I'm trying to get all of the pairs of data and get them into a single, 2D array that I can then put into google sheets using google sheets script.
Here's the code I'm starting with:
function parseTheAPI(rawResponse){
var theparsedJSONdata = JSON.parse(rawResponse)
console.log(theparsedJSONdata)
return theparsedJSONdata
};
Here's the response from the console:
{ '0xabcxyz':
{ products: [ [Object] ],
meta: [ [Object], [Object], [Object] ] } }
There's lots more actual data where it says Object
. I understand how to get at individual pieces of data once I know the contents of the object. If the contents of the object change, however, the code would break.
I want to dynamically get at all the various information pairs and use them.
I've looked at SO questions like this and this and tried to figure out if I can just see that data. Here's my attempt at using recursion to figure what's inside:
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)
}
}
};
I get some of the data printed, but I don't know how to get to the next level. I don't understand what's coming out of each part of the "if" test. Here's an example of one of the nested objects printed in the console:
{ type: 'claimable',
category: 'claimable',
address: '0x00000000123456lkjlkj',
symbol: 'WMATIC',
decimals: 18,
label: 'Claimable WMATIC',
img: 'networks/polygon/0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270.png',
price: 1.64,
balance: 0.03567595026086894,
balanceRaw: '35675950260868942',
balanceUSD: 0.05850855842782506 }
How do I figure out what is in the JSON data, at any depth, see if it's an array or object, and put the extracted pairs into a single array?
EDIT:
I'm trying to get this data into two rows in a google sheet. so I can track individual pieces every day. The "price", the "balanceRaw"...
The issue is the user may be participating in different AAVE pools from day to day; i.e. this JSON object might have lots of different information every day.
Here's the raw data from the API:
{"0x0000123456abcdef":{"products":[{"label":"Aave V2","assets":[{"type":"interest-bearing","category":"deposit","address":"0x27F8D03b3a2196956ED754baDc28D73be8830A6e","symbol":"DAI","decimals":18,"label":"DAI in Aave","img":"networks/polygon/0x8f3cf7ad23cd3cadbd9735aff958023239c6a063.png","protocol":"aave","protocolDisplay":"Aave","protocolSymbol":"AAVE","price":0.999254,"apy":0.027310184786005925,"balanceRaw":"2668910745526108687981","balance":2668.910745526109,"balanceUSD":2666.9197381099466},{"type":"claimable","category":"claimable","address":"0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270","symbol":"WMATIC","decimals":18,"label":"Claimable WMATIC","img":"networks/polygon/0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270.png","price":1.65,"balance":0.042818503811087726,"balanceRaw":"42818503811087730","balanceUSD":0.07065053128829474}],"meta":[]}],"meta":[{"label":"Total","value":2666.990388641235,"type":"dollar"},{"label":"Assets","value":2666.990388641235,"type":"dollar"},{"label":"Debt","value":0,"type":"dollar"}]}}
EDIT 2
I've tried the this code from this SO question:
function flattenJsonObject (parsedJson){
console.log("test first")
Object.flatten = function(parsedJson) {
var result = {};
function recurse (cur, prop) {
console.log("test")
if (Object(cur) !== cur) {
result[prop] = cur;
} else if (Array.isArray(cur)) {
for(var i=0, l=cur.length; i<l; i++)
recurse(cur[i], prop + "[" + i + "]");
if (l == 0)
result[prop] = [];
} else {
var isEmpty = true;
for (var p in cur) {
isEmpty = false;
recurse(cur[p], prop ? prop+"."+p : p);
}
if (isEmpty && prop)
result[prop] = {};
}
}
recurse(parsedJson, "");
console.log(result)
return result;
}
}
Something is not working because the second and third console.log are not being printed in the console.