Parse JSON URL time-series in Python

Looking to obtain the latest "last" price. Also, I would like to capture the average of the last 4 time-period prints. Used JSONLint to validate the JSON string is valid. URL omitted due to privacy concerns.

The desired result is to have: I.e: Indicator_mean_Intra = last price print(0.74056 + 4 'last' prices mean(0.74056, 0.740165,0.738415,0.845215)/2

import json
from pprint import pprint

with open('URL') as g:
    USDCAN = json.load(g)



   pprint(USDCAN )
   ## With USDCAN , you can now also find values like so:
    USDCAN ["requestedSymbol"][0]["id"]
    USDCAN ["requestedSymbol"]["data"]["intervals"] <--- what about factoring for time? ["last"]

Output with middle data removed to make more concise:

[{
    "requestedSymbol": "USD/CAD",
    "success": true,
    "data": {
        "symbol": "USD/CAD",
        "name": "USD/CAD",
        "baseCurrency": "USD",
        "intervals": [{
            "start": "2018-05-13T16:00:00+00:00",
            "end": "2018-05-13T19:59:59.999+00:00",
            "open": 1.27959,
            "high": 1.27959,
            "low": 1.27804,
            "last": 1.278225
                    }, {
            "start": "2018-05-16T04:00:00+00:00",
            "end": "2018-05-16T07:59:59.999+00:00",
            "open": 1.285645,
            "high": 1.28625,
            "low": 1.285645,
            "last": 1.28625
        }]
    }
}, {
    "requestedSymbol": "USD/EUR",
    "success": true,
    "data": {
        "symbol": "USD/EUR",
        "name": "USD/EUR",
        "baseCurrency": "USD",
        "intervals": [{
            "start": "2018-05-13T16:00:00+00:00",
            "end": "2018-05-13T19:59:59.999+00:00",
            "open": 0.83738,
            "high": 0.837485,
            "low": 0.83689,
            "last": 0.836995
        }, {
            "start": "2018-05-13T20:00:00+00:00",
            "end": "2018-05-13T23:59:59.999+00:00",
            "open": 0.837,
            "high": 0.837555,
            "low": 0.8364,
            "last": 0.836635
        }, {
            "start": "2018-05-14T00:00:00+00:00",
            "end": "2018-05-14T03:59:59.999+00:00",
            "open": 0.836645,
            "high": 0.8368,
            "low": 0.8355,
            "last": 0.835805
        }, {
            "start": "2018-05-14T04:00:00+00:00",
            "end": "2018-05-14T07:59:59.999+00:00",
            "open": 0.835795,
            "high": 0.836725,
            "low": 0.8344115,
            "last": 0.8348295
        }, {
            "start": "2018-05-14T08:00:00+00:00",
            "end": "2018-05-14T11:59:59.999+00:00",
            "open": 0.834785,
            "high": 0.835505,
            "low": 0.8339,
            "last": 0.8343415
        }, {
            "start": "2018-05-14T12:00:00+00:00",
            "end": "2018-05-14T15:59:59.999+00:00",
            "open": 0.834405,
            "high": 0.8360155,
            "low": 0.8335,
            "last": 0.835045
        }, {
            "start": "2018-05-14T16:00:00+00:00",
            "end": "2018-05-14T19:59:59.999+00:00",
            "open": 0.835025,
            "high": 0.8379,
            "low": 0.8348,
            "last": 0.837655
        }, {
            "start": "2018-05-14T20:00:00+00:00",
            "end": "2018-05-14T23:59:59.999+00:00",
            "open": 0.837645,
            "high": 0.8388,
            "low": 0.837485,
            "last": 0.8382
        }, {
            "start": "2018-05-15T00:00:00+00:00",
            "end": "2018-05-15T03:59:59.999+00:00",
            "open": 0.838125,
            "high": 0.8384,
            "low": 0.8376,
            "last": 0.837995
        }, {
            "start": "2018-05-15T04:00:00+00:00",
            "end": "2018-05-15T07:59:59.999+00:00",
            "open": 0.838005,
            "high": 0.839605,
            "low": 0.837556,
            "last": 0.8386
        }, {
            "start": "2018-05-15T08:00:00+00:00",
            "end": "2018-05-15T11:59:59.999+00:00",
            "open": 0.838715,
            "high": 0.842015,
            "low": 0.8381875,
            "last": 0.841845
        }, {
            "start": "2018-05-15T12:00:00+00:00",
            "end": "2018-05-15T15:59:59.999+00:00",
            "open": 0.841865,
            "high": 0.846,
            "low": 0.841015,
            "last": 0.842895
        }, {
            "start": "2018-05-15T16:00:00+00:00",
            "end": "2018-05-15T19:59:59.999+00:00",
            "open": 0.842885,
            "high": 0.8449,
            "low": 0.841999,
            "last": 0.844395
        }, {
            "start": "2018-05-15T20:00:00+00:00",
            "end": "2018-05-15T23:59:59.999+00:00",
            "open": 0.844415,
            "high": 0.846225,
            "low": 0.844085,
            "last": 0.84598
        }, {
            "start": "2018-05-16T00:00:00+00:00",
            "end": "2018-05-16T03:59:59.999+00:00",
            "open": 0.84598,
            "high": 0.84633,
            "low": 0.84497,
            "last": 0.845065
        }, {
            "start": "2018-05-16T04:00:00+00:00",
            "end": "2018-05-16T07:59:59.999+00:00",
            "open": 0.84509,
            "high": 0.8462745,
            "low": 0.844844,
            "last": 0.845215
        }]
    }
}, {
    "requestedSymbol": "USD/GBP",
    "success": true,
    "data": {
        "symbol": "USD/GBP",
        "name": "USD/GBP",
        "baseCurrency": "USD",
        "intervals": [{
            "start": "2018-05-13T08:00:00+00:00",
            "end": "2018-05-13T11:59:59.999+00:00",
            "open": 0.738415,
            "high": 0.738415,
            "low": 0.738415,
            "last": 0.738415
        }, {
            "start": "2018-05-15T12:00:00+00:00",
            "end": "2018-05-15T15:59:59.999+00:00",
            "open": 0.739895,
            "high": 0.7435,
            "low": 0.7393,
            "last": 0.740165
        }, {
            "start": "2018-05-16T04:00:00+00:00",
            "end": "2018-05-16T07:59:59.999+00:00",
            "open": 0.740605,
            "high": 0.741,
            "low": 0.7402,
            "last": 0.74056
        }]
    }
}]

For conciseness I deleted some middle values to not make the script too long. Thank you for your time.

1 answer

  • answered 2018-05-16 06:10 shahaf

    I'm sorry I still didn't understood exactly what do you want, so I'll scratch a draft, hopefully it will be in the right direction of what you want to achive

    last_arr = list()
    for i in json_res[-1::-1]:
        for interval in i['data']['intervals'][-1::-1]:
            last_arr.append(interval['last'])
    
            if len(last_arr) == 4:
                break
    
        if len(last_arr) == 4:
            break
    
    print(last_arr)
    

    output

    [0.74056, 0.740165, 0.738415, 0.845215]
    

    a better code will be to obtain a list of all lasts and get a sub-set like

    for i in json_res[-1::-1]:
        for interval in i['data']['intervals'][-1::-1]:
            last_arr.append(interval['last'])
    
    print(las_arr[:4]) // get 4 first elements in the list
    

    this option although less efficent (iterating all over the array) is more simple and robust, actually (performance is pretty much the same for common json responses O(1) )

    Note: keep in mind the loops going in reverse order, and build a list of last ordered elements, you can iterate in regular order and get the last elements of the list