Listing all the nested keys from hash

{
“reports”: [
{
“columnHeader”: {
“dimensions”: [
“ga:dateHour”,
“ga:browser”,
“ga:browserVersion”,
“ga:operatingSystem”,
“ga:mobileDeviceInfo”,
“ga:deviceCategory”,
“ga:browserSize”
],
“metricHeader”: {
“metricHeaderEntries”: [
{
“name”: “ga:users”,
“type”: “INTEGER”
},
{
“name”: “ga:sessions”,
“type”: “INTEGER”
},
{
“name”: “ga:bounces”,
“type”: “INTEGER”
},
{
“name”: “ga:pageviews”,
“type”: “INTEGER”
},
{
“name”: “ga:transactions”,
“type”: “INTEGER”
},
{
“name”: “ga:transactionRevenue”,
“type”: “CURRENCY”
},
{
“name”: “ga:pageLoadTime”,
“type”: “INTEGER”
},
{
“name”: “ga:domInteractiveTime”,
“type”: “INTEGER”
},
{
“name”: “ga:domContentLoadedTime”,
“type”: “INTEGER”
},
{
“name”: “ga:sessionDuration”,
“type”: “TIME”
}
]
}
},
“data”: {
“rows”: [
{
“dimensions”: [
“2020011908”,
“Safari (in-app)”,
“(not set)”,
“iOS”,
“Apple iPhone 8 Plus”,
“mobile”,
“410x630”
],
“metrics”: [
{
“values”: [
“2”,
“2”,
“1”,
“4”,
“0”,
“0.0”,
“0”,
“0”,
“0”,
“55.0”
]
}
]
}
],
“totals”: [
{
“values”: [
“917”,
“903”,
“430”,
“1924”,
“30”,
“1948.21”,
“44279”,
“24547”,
“24549”,
“73585.0”
]
}
],
“rowCount”: 788,
“minimums”: [
{
“values”: [
“1”,
“0”,
“0”,
“1”,
“0”,
“0.0”,
“0”,
“0”,
“0”,
“0.0”
]
}
],
“maximums”: [
{
“values”: [
“4”,
“4”,
“3”,
“17”,
“1”,
“209.84”,
“9708”,
“5697”,
“5698”,
“2527.0”
]
}
]
},
“nextPageToken”: “206”
}
]
}

json_from_file = File.read(“test.json”)
hash = JSON.parse(json_from_file)
here i want to list all the keys from hash

Im not sure how to solve that as you have nested hashes within hashes but this code at least prints it nicely for you:

require 'awesome_print'
require "json"

file = File.open("test.json")
data = JSON.load file

ap data

data.each do |key,value|
  puts key
end

notice if you comment out “ap data” it just returns “reports” as that is the first and only key in the first hash.

{
    "reports" => [
        [0] {
             "columnHeader" => {
                  "dimensions" => [
                    [0] "ga:dateHour",
                    [1] "ga:browser",
                    [2] "ga:browserVersion",
                    [3] "ga:operatingSystem",
                    [4] "ga:mobileDeviceInfo",
                    [5] "ga:deviceCategory",
                    [6] "ga:browserSize"
                ],
                "metricHeader" => {
                    "metricHeaderEntries" => [
                        [0] {
                            "name" => "ga:users",
                            "type" => "INTEGER"
                        },
                        [1] {
                            "name" => "ga:sessions",
                            "type" => "INTEGER"
                        },
                        [2] {
                            "name" => "ga:bounces",
                            "type" => "INTEGER"
                        },
                        [3] {
                            "name" => "ga:pageviews",
                            "type" => "INTEGER"
                        },
                        [4] {
                            "name" => "ga:transactions",
                            "type" => "INTEGER"
                        },
                        [5] {
                            "name" => "ga:transactionRevenue",
                            "type" => "CURRENCY"
                        },
                        [6] {
                            "name" => "ga:pageLoadTime",
                            "type" => "INTEGER"
                        },
                        [7] {
                            "name" => "ga:domInteractiveTime",
                            "type" => "INTEGER"
                        },
                        [8] {
                            "name" => "ga:domContentLoadedTime",
                            "type" => "INTEGER"
                        },
                        [9] {
                            "name" => "ga:sessionDuration",
                            "type" => "TIME"
                        }
                    ]
                }
            },
                     "data" => {
                    "rows" => [
                    [0] {
                        "dimensions" => [
                            [0] "2020011908",
                            [1] "Safari (in-app)",
                            [2] "(not set)",
                            [3] "iOS",
                            [4] "Apple iPhone 8 Plus",
                            [5] "mobile",
                            [6] "410x630"
                        ],
                           "metrics" => [
                            [0] {
                                "values" => [
                                    [0] "2",
                                    [1] "2",
                                    [2] "1",
                                    [3] "4",
                                    [4] "0",
                                    [5] "0.0",
                                    [6] "0",
                                    [7] "0",
                                    [8] "0",
                                    [9] "55.0"
                                ]
                            }
                        ]
                    }
                ],
                  "totals" => [
                    [0] {
                        "values" => [
                            [0] "917",
                            [1] "903",
                            [2] "430",
                            [3] "1924",
                            [4] "30",
                            [5] "1948.21",
                            [6] "44279",
                            [7] "24547",
                            [8] "24549",
                            [9] "73585.0"
                        ]
                    }
                ],
                "rowCount" => 788,
                "minimums" => [
                    [0] {
                        "values" => [
                            [0] "1",
                            [1] "0",
                            [2] "0",
                            [3] "1",
                            [4] "0",
                            [5] "0.0",
                            [6] "0",
                            [7] "0",
                            [8] "0",
                            [9] "0.0"
                        ]
                    }
                ],
                "maximums" => [
                    [0] {
                        "values" => [
                            [0] "4",
                            [1] "4",
                            [2] "3",
                            [3] "17",
                            [4] "1",
                            [5] "209.84",
                            [6] "9708",
                            [7] "5697",
                            [8] "5698",
                            [9] "2527.0"
                        ]
                    }
                ]
            },
            "nextPageToken" => "206"
        }
    ]
}
reports

It might be enough code to get you started though. You might have to iterate over each key and do the same block.

Hope that helps