如何在 Google 工作表中解析嵌套 JSON 结构并在数据不可用时回退?

How to parse in Google Sheets a nested JSON structure with fallback when the data isn't available?

我正在以 JSON 文件的形式获取 Yahoo Finance 数据(通过 YahooFinancials python API),我希望能够以智能方式解析数据喂养我的 Google Sheet.

对于这个例子,我有兴趣在 "date" 嵌套结构下获取 "cash" 变量。但正如您将看到的,有时第一个日期下没有 "cash" 变量,所以我希望 script/formula 去获取第二个日期结构下的 "cash" 变量。

这是 JSON 代码的示例 1:

{ "balanceSheetHistoryQuarterly": {
        "ABBV": [
            {
                "2018-12-31": {
                    "totalStockholderEquity": -2921000000,
                    "netTangibleAssets": -45264000000
                }
            },
            {
                "2018-09-30": {
                    "intangibleAssets": 26625000000,
                    "capitalSurplus": 14680000000,
                    "totalLiab": 69085000000,
                    "totalStockholderEquity": -2921000000,
                    "otherCurrentLiab": 378000000,
                    "totalAssets": 66164000000,
                    "commonStock": 18000000,
                    "otherCurrentAssets": 112000000,
                    "retainedEarnings": 6789000000,
                    "otherLiab": 16511000000,
                    "goodWill": 15718000000,
                    "treasuryStock": -24408000000,
                    "otherAssets": 943000000,
                    "cash": 8015000000,
                    "totalCurrentLiabilities": 15387000000,
                    "shortLongTermDebt": 1026000000,
                    "otherStockholderEquity": -2559000000,
                    "propertyPlantEquipment": 2950000000,
                    "totalCurrentAssets": 18465000000,
                    "longTermInvestments": 1463000000,
                    "netTangibleAssets": -45264000000,
                    "shortTermInvestments": 770000000,
                    "netReceivables": 5780000000,
                    "longTermDebt": 37187000000,
                    "inventory": 1786000000,
                    "accountsPayable": 10981000000
                }
            },
            {
                "2018-06-30": {
                    "intangibleAssets": 26903000000,
                    "capitalSurplus": 14596000000,
                    "totalLiab": 65016000000,
                    "totalStockholderEquity": -3375000000,
                    "otherCurrentLiab": 350000000,
                    "totalAssets": 61641000000,
                    "commonStock": 18000000,
                    "otherCurrentAssets": 128000000,
                    "retainedEarnings": 5495000000,
                    "otherLiab": 16576000000,
                    "goodWill": 15692000000,
                    "treasuryStock": -23484000000,
                    "otherAssets": 909000000,
                    "cash": 3547000000,
                    "totalCurrentLiabilities": 17224000000,
                    "shortLongTermDebt": 3026000000,
                    "otherStockholderEquity": -2639000000,
                    "propertyPlantEquipment": 2787000000,
                    "totalCurrentAssets": 13845000000,
                    "longTermInvestments": 1505000000,
                    "netTangibleAssets": -45970000000,
                    "shortTermInvestments": 196000000,
                    "netReceivables": 5793000000,
                    "longTermDebt": 31216000000,
                    "inventory": 1580000000,
                    "accountsPayable": 10337000000
                }
            },
            {
                "2018-03-31": {
                    "intangibleAssets": 27230000000,
                    "capitalSurplus": 14519000000,
                    "totalLiab": 65789000000,
                    "totalStockholderEquity": 3553000000,
                    "otherCurrentLiab": 125000000,
                    "totalAssets": 69342000000,
                    "commonStock": 18000000,
                    "otherCurrentAssets": 17000000,
                    "retainedEarnings": 4977000000,
                    "otherLiab": 17250000000,
                    "goodWill": 15880000000,
                    "treasuryStock": -15961000000,
                    "otherAssets": 903000000,
                    "cash": 9007000000,
                    "totalCurrentLiabilities": 17058000000,
                    "shortLongTermDebt": 6024000000,
                    "otherStockholderEquity": -2630000000,
                    "propertyPlantEquipment": 2828000000,
                    "totalCurrentAssets": 20444000000,
                    "longTermInvestments": 2057000000,
                    "netTangibleAssets": -39557000000,
                    "shortTermInvestments": 467000000,
                    "netReceivables": 5841000000,
                    "longTermDebt": 31481000000,
                    "inventory": 1738000000,
                    "accountsPayable": 10542000000
                }
            }
        ]
}
}

第一个日期结构(2018-12-31 下)不包含 cash 变量。因此,我希望 Google sheet 在 2018-09-30 中搜索相同的数据,如果不可用,则在 2018-06-30 中搜索。 或者只扫描嵌套结构日期并获取将找到的第一个 "cash" 事件。

基本上,我想知道如何跳过日期变量的名称(即 2018-12-31),因为它并不重要,只需让公式寻找第一个可用的 "cash"变量。

主要问题回顾

如果您需要有关此问题的更多背景信息,请告诉我,在此先感谢您的帮助:)

编辑:这是我现在在价差sheet单元格中使用的IMPORTJSON公式。

=ImportJSON("https://api.myjson.com/bins/8mxvi", "/financial/balanceSheetHistoryQuarterly/ABBV/2018-31-12/cash", "noHeaders")

显然,这个 returns 一个错误,因为该日期下没有任何内容。 JSON也是我刚才用的有效link。

=REGEXEXTRACT(FILTER(
 TRANSPOSE(SPLIT(SUBSTITUTE(A1, ","&CHAR(10), "×"), "×")), 
 ISNUMBER(SEARCH("*"&"cash"&"*",
 TRANSPOSE(SPLIT(SUBSTITUTE(A1, ","&CHAR(10), "×"), "×"))))), ": (.+)")


=INDEX(ARRAYFORMULA(SUBSTITUTE(REGEXEXTRACT(FILTER(TRANSPOSE(SPLIT(SUBSTITUTE(
 TRANSPOSE(IMPORTDATA("https://api.myjson.com/bins/8mxvi")), ","&CHAR(10), "×"), "×")), 
 ISNUMBER(SEARCH("*"&"cash"&"*", TRANSPOSE(SPLIT(SUBSTITUTE(
 TRANSPOSE(IMPORTDATA("https://api.myjson.com/bins/8mxvi")), ","&CHAR(10), "×"), "×"))))),
  ":(.+)"), ",", "")), 1, 1)