Please advise on nested json with nested dynamic keys

Paula DiTallo
Paula DiTallo used Ask the Experts™
on
How do I parse Nested JSON with NESTED dynamic keys in Android kotlin, Moshi and Retrofit?

I get this JSON from alpha-vantage.

Format example:
{
    "Meta Data": {
        "1. Information": "Intraday (15min) open, high, low, close prices and volume",
        "2. Symbol": "AAME",
        "3. Last Refreshed": "2019-11-18 16:00:00",
        "4. Interval": "15min",
        "5. Output Size": "Compact",
        "6. Time Zone": "US/Eastern"
     },
    "Time Series (15min)": {//Dynamic - > Time Series (5min) / Time Series (30min)
        "2019-11-18 16:00:00": {//Dynamic
            "1. open": "1.6700",
            "2. high": "1.6700",
            "3. low": "1.5700",
            "4. close": "1.5700",
            "5. volume": "1521"
        },
        "2019-11-18 15:45:00": {//Dynamic
            "1. open": "1.6600",
            "2. high": "1.7400",
            "3. low": "1.6600",
            "4. close": "1.7400",
            "5. volume": "355"
        }
    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
you loop over all the keys

for(key in js("Object").keys(your_json)) {
        // you get each iteration a new key, in one of them key is  "2019-11-18 15:45:00"
}

Open in new window

Integration developer
Commented:
For anyone coming behind me:

private void parseJson(String jsonString) {
    try {
        //  pass your response json string here
        JSONObject object = new JSONObject(jsonString);

        Iterator<String> it = object.keys();
        while (it.hasNext()) {
            String key = it.next();
            try {
                if (object.get(key) instanceof JSONObject) {
                    Log.e("Main OBJ", key);
                    JSONObject object2 = object.getJSONObject(key);
                    Iterator<String> it2 = object2.keys();
                    while (it2.hasNext()) {
                        String key2 = it2.next();
                        if (object2.get(key2) instanceof JSONObject) {
                            Log.e("Sub OBJ", key2);
                            JSONObject object3 = object2.getJSONObject(key2);
                            Iterator<String> it3 = object3.keys();
                            while (it3.hasNext()) {
                                String key4 = it3.next();
                                Log.e("Values", key4);
                            }
                        } else {
                            Log.e("Values", object2.getString(key2));
                        }
                    }

                } else {
                    System.out.println(key + ":" + object.getString(key));
                }
            } catch (Throwable e) {
                try {
                    System.out.println(key + ":" + object.getString(key));
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
                e.printStackTrace();
            }
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

-----------------------------------------  output looks like this ------------------------------------------------

Main OBJ: Meta Data
Values: Intraday (15min) open, high, low, close prices and volume
Values: AAME
Values: 2019-11-18 16:00:00
Values: 15min
Values: Compact
Values: US/Eastern
Main OBJ: Time Series (15min)
Sub OBJ: 2019-11-18 16:00:00
Values: 1. open
Values: 2. high
Values: 3. low
Values: 4. close
Values: 5. volume
Sub OBJ: 2019-11-18 15:45:00
Values: 1. open
Values: 2. high
Values: 3. low
Values: 4. close
Values: 5. volume

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial