-1

I have an object that prints this to console:

{
    "symbol": "GOOGL",
    "annualReports": [
        {
            "fiscalDateEnding": "2020-12-31",
            "reportedCurrency": "USD",
            "operatingCashflow": "65124000000",
            "paymentsForOperatingActivities": "2004000000",
            "proceedsFromOperatingActivities": "None",
            "changeInOperatingLiabilities": "6833000000",
            "changeInOperatingAssets": "7854000000",
            "depreciationDepletionAndAmortization": "13697000000",
            "capitalExpenditures": "22281000000",
            "changeInReceivables": "6524000000",
            "changeInInventory": "None",
            "profitLoss": "40269000000",
            "cashflowFromInvestment": "-32773000000",
            "cashflowFromFinancing": "-24408000000",
            "proceedsFromRepaymentsOfShortTermDebt": "None",
            "paymentsForRepurchaseOfCommonStock": "31149000000",
            "paymentsForRepurchaseOfEquity": "31149000000",
            "paymentsForRepurchaseOfPreferredStock": "None",
            "dividendPayout": "None",
            "dividendPayoutCommonStock": "None",
            "dividendPayoutPreferredStock": "None",
            "proceedsFromIssuanceOfCommonStock": "None",
            "proceedsFromIssuanceOfLongTermDebtAndCapitalSecuritiesNet": "None",
            "proceedsFromIssuanceOfPreferredStock": "None",
            "proceedsFromRepurchaseOfEquity": "-31149000000",
            "proceedsFromSaleOfTreasuryStock": "None",
            "changeInCashAndCashEquivalents": "7943000000",
            "changeInExchangeRate": "None",
            "netIncome": "40269000000"
        },
        {
            "fiscalDateEnding": "2019-12-31",
            "reportedCurrency": "USD",
            "operatingCashflow": "54520000000",
            "paymentsForOperatingActivities": "1661000000",
            "proceedsFromOperatingActivities": "None",
            "changeInOperatingLiabilities": "7635000000",
            "changeInOperatingAssets": "4961000000",
            "depreciationDepletionAndAmortization": "11781000000",
            "capitalExpenditures": "23548000000",
            "changeInReceivables": "4340000000",
            "changeInInventory": "None",
            "profitLoss": "34343000000",
            "cashflowFromInvestment": "-29491000000",
            "cashflowFromFinancing": "-23209000000",
            "proceedsFromRepaymentsOfShortTermDebt": "None",
            "paymentsForRepurchaseOfCommonStock": "18396000000",
            "paymentsForRepurchaseOfEquity": "18396000000",
            "paymentsForRepurchaseOfPreferredStock": "None",
            "dividendPayout": "None",
            "dividendPayoutCommonStock": "None",
            "dividendPayoutPreferredStock": "None",
            "proceedsFromIssuanceOfCommonStock": "None",
            "proceedsFromIssuanceOfLongTermDebtAndCapitalSecuritiesNet": "None",
            "proceedsFromIssuanceOfPreferredStock": "None",
            "proceedsFromRepurchaseOfEquity": "-18396000000",
            "proceedsFromSaleOfTreasuryStock": "None",
            "changeInCashAndCashEquivalents": "1820000000",
            "changeInExchangeRate": "None",
            "netIncome": "34343000000"
        },
        {
            "fiscalDateEnding": "2018-12-31",
            "reportedCurrency": "USD",
            "operatingCashflow": "47971000000",
            "paymentsForOperatingActivities": "69000000",
            "proceedsFromOperatingActivities": "None",
            "changeInOperatingLiabilities": "10052000000",
            "changeInOperatingAssets": "3376000000",
            "depreciationDepletionAndAmortization": "9035000000",
            "capitalExpenditures": "25139000000",
            "changeInReceivables": "2169000000",
            "changeInInventory": "None",
            "profitLoss": "30736000000",
            "cashflowFromInvestment": "-28504000000",
            "cashflowFromFinancing": "-13179000000",
            "proceedsFromRepaymentsOfShortTermDebt": "None",
            "paymentsForRepurchaseOfCommonStock": "9075000000",
            "paymentsForRepurchaseOfEquity": "9075000000",
            "paymentsForRepurchaseOfPreferredStock": "None",
            "dividendPayout": "None",
            "dividendPayoutCommonStock": "None",
            "dividendPayoutPreferredStock": "None",
            "proceedsFromIssuanceOfCommonStock": "None",
            "proceedsFromIssuanceOfLongTermDebtAndCapitalSecuritiesNet": "None",
            "proceedsFromIssuanceOfPreferredStock": "None",
            "proceedsFromRepurchaseOfEquity": "-9075000000",
            "proceedsFromSaleOfTreasuryStock": "None",
            "changeInCashAndCashEquivalents": "5986000000",
            "changeInExchangeRate": "-302000000",
            "netIncome": "30736000000"
        },
        {
            "fiscalDateEnding": "2017-12-31",
            "reportedCurrency": "USD",
            "operatingCashflow": "37091000000",
            "paymentsForOperatingActivities": "84000000",
            "proceedsFromOperatingActivities": "None",
            "changeInOperatingLiabilities": "6012000000",
            "changeInOperatingAssets": "5932000000",
            "depreciationDepletionAndAmortization": "6915000000",
            "capitalExpenditures": "13184000000",
            "changeInReceivables": "3768000000",
            "changeInInventory": "None",
            "profitLoss": "12506000000",
            "cashflowFromInvestment": "-31401000000",
            "cashflowFromFinancing": "-8298000000",
            "proceedsFromRepaymentsOfShortTermDebt": "None",
            "paymentsForRepurchaseOfCommonStock": "4846000000",
            "paymentsForRepurchaseOfEquity": "4846000000",
            "paymentsForRepurchaseOfPreferredStock": "None",
            "dividendPayout": "None",
            "dividendPayoutCommonStock": "None",
            "dividendPayoutPreferredStock": "None",
            "proceedsFromIssuanceOfCommonStock": "None",
            "proceedsFromIssuanceOfLongTermDebtAndCapitalSecuritiesNet": "None",
            "proceedsFromIssuanceOfPreferredStock": "None",
            "proceedsFromRepurchaseOfEquity": "-4846000000",
            "proceedsFromSaleOfTreasuryStock": "None",
            "changeInCashAndCashEquivalents": "-2203000000",
            "changeInExchangeRate": "405000000",
            "netIncome": "12662000000"
        },
        {
            "fiscalDateEnding": "2016-12-31",
            "reportedCurrency": "USD",
            "operatingCashflow": "36036000000",
            "paymentsForOperatingActivities": "84000000",
            "proceedsFromOperatingActivities": "None",
            "changeInOperatingLiabilities": "1848000000",
            "changeInOperatingAssets": "2578000000",
            "depreciationDepletionAndAmortization": "6144000000",
            "capitalExpenditures": "10212000000",
            "changeInReceivables": "2578000000",
            "changeInInventory": "None",
            "profitLoss": "19276000000",
            "cashflowFromInvestment": "-31165000000",
            "cashflowFromFinancing": "-8332000000",
            "proceedsFromRepaymentsOfShortTermDebt": "None",
            "paymentsForRepurchaseOfCommonStock": "3693000000",
            "paymentsForRepurchaseOfEquity": "3693000000",
            "paymentsForRepurchaseOfPreferredStock": "None",
            "dividendPayout": "None",
            "dividendPayoutCommonStock": "None",
            "dividendPayoutPreferredStock": "None",
            "proceedsFromIssuanceOfCommonStock": "None",
            "proceedsFromIssuanceOfLongTermDebtAndCapitalSecuritiesNet": "None",
            "proceedsFromIssuanceOfPreferredStock": "None",
            "proceedsFromRepurchaseOfEquity": "-3395000000",
            "proceedsFromSaleOfTreasuryStock": "None",
            "changeInCashAndCashEquivalents": "-3631000000",
            "changeInExchangeRate": "-170000000",
            "netIncome": "19478000000"
        }
    ],

I'm wondering how I would get specific variables for each annual report. For example, I need to make a Pandas dataframe of just 'netIncome' from each report. How would I parse such an object and get this data?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Looks like it conforms with [**`json`**](https://docs.python.org/3/library/json.html). – Peter Wood Jun 15 '21 at 18:47
  • 1
    If you have this as string, its probably a json-string. Load it with `json.loads()` and then you have a normal dictionary. Which you can access like `loaded_data['annualReports']`. – Sven Eberth Jun 15 '21 at 18:48
  • I guess this isn't the whole object, because it doesn't close the initial brace. I had assumed `dict`, but what does `type(obj)` give you? – Ben Y Jun 15 '21 at 18:50
  • @SvenEberth any tips on how to load it using json.loads()? Is it just a one line command? Does it need any parameters for which variables need to be loaded? Thanks! – thesmashten Jun 15 '21 at 18:53
  • @BenY printing type(obj) yields – thesmashten Jun 15 '21 at 18:54
  • It isn't valid JSON, it's missing a closing `}` and the last `,` shouldn't be there. – norie Jun 15 '21 at 18:57
  • I have updated my answer, unfortunately, it's sort of out-of-order, but I hope this helps. – Ben Y Jun 15 '21 at 18:57
  • Open the link in Peter Wood's comment or take a look at Ben Y's answer. `json` is a standard python module. It can be used via CLI, but I think in general you would use the methods. – Sven Eberth Jun 15 '21 at 18:58
  • I'm assuming the whole string is much longer, and properly closes the opening brace, correct? – Ben Y Jun 15 '21 at 18:58

1 Answers1

0

This looks like a dict where the key symbol indicates which stock, and then the key annualReports contains a list of dict.

The only thing resembling a DataFrame, would be what is contained in the annualReports member.

df = DataFrame(obj['annualReports'])

Might be your first step, then you can take a look at df['netIncome])`.

Edit:

The title didn't match the question description, so I will also assume then, that the input is a string. In which case you would need to first import the JSON into a Python object (dict).

from json import loads

obj = loads(source_string)
Ben Y
  • 913
  • 6
  • 18
  • You missed the title. It's a string, not a dict. – Sven Eberth Jun 15 '21 at 18:50
  • Nowhere in the title does it say "string". This is the first line: "I have an object that prints this to console:" I would like to know what this object is. I wasn't sure of the notation , so I didn't make that assumption. – Ben Y Jun 15 '21 at 18:52
  • _ object_ is a string. – Sven Eberth Jun 15 '21 at 18:55
  • 1
    Then my answer has been updated to address the ambiguity.. – Ben Y Jun 15 '21 at 18:55
  • If it is a string, why was it edited to look like a `dict` itself? In this case I think editing and prettifying it could confuse prospective answerers. – Ben Y Jun 15 '21 at 18:56