pythonjsonjsonresponse

how to break down json response into multiple parts specific to the name value?


hi i am making an api call to myfxbook api and in return i am receiving a json response with too much data , is it possible to break down the big response to multiple smaller parts with respect to what the name value is ? Here is about 30% of the Json response:

[{'name': 'EURUSD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 22685.66, 'longVolume': 47762.79, 'longPositions': 89968, 'shortPositions': 56612, 'totalPositions': 146580, 'avgShortPrice': 1.1678, 'avgLongPrice': 1.199}, {'name': 'GBPUSD', 'shortPercentage': 54, 'longPercentage': 46, 'shortVolume': 6462.95, 'longVolume': 5587.2, 'longPositions': 17783, 'shortPositions': 22240, 'totalPositions': 40023, 'avgShortPrice': 1.3592, 'avgLongPrice': 1.3929}, {'name': 'USDJPY', 'shortPercentage': 89, 'longPercentage': 11, 'shortVolume': 32447.66, 'longVolume': 4081.71, 'longPositions': 10941, 'shortPositions': 58722, 'totalPositions': 69663, 'avgShortPrice': 106.7526, 'avgLongPrice': 108.6437}, {'name': 'GBPJPY', 'shortPercentage': 79, 'longPercentage': 21, 'shortVolume': 2725.22, 'longVolume': 711.99, 'longPositions': 3210, 'shortPositions': 9743, 'totalPositions': 12953, 'avgShortPrice': 146.1479, 'avgLongPrice': 149.2243}, {'name': 'USDCAD', 'shortPercentage': 47, 'longPercentage': 53, 'shortVolume': 6235.58, 'longVolume': 6930.97, 'longPositions': 16121, 'shortPositions': 13276, 'totalPositions': 29397, 'avgShortPrice': 1.2658, 'avgLongPrice': 1.3025}, {'name': 'EURAUD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 562.13, 'longVolume': 1170.82, 'longPositions': 4444, 'shortPositions': 3004, 'totalPositions': 7448, 'avgShortPrice': 1.5434, 'avgLongPrice': 1.5816}, {'name': 'EURJPY', 'shortPercentage': 74, 'longPercentage': 26, 'shortVolume': 10500.41, 'longVolume': 3672.26, 'longPositions': 8166, 'shortPositions': 25254, 'totalPositions': 33420, 'avgShortPrice': 126.1881, 'avgLongPrice': 128.6925}, {'name': 'AUDCAD', 'shortPercentage': 48, 'longPercentage': 52, 'shortVolume': 1559.83, 'longVolume': 1699.36, 'longPositions': 5943, 'shortPositions': 6278, 'totalPositions': 12221, 'avgShortPrice': 0.9561, 'avgLongPrice': 0.979}, {'name': 'AUDJPY', 'shortPercentage': 83, 'longPercentage': 17, 'shortVolume': 1614.66, 'longVolume': 322.98, 'longPositions': 1905, 'shortPositions': 6048, 'totalPositions': 7953, 'avgShortPrice': 78.9751, 'avgLongPrice': 83.5306}, {'name': 'AUDNZD', 'shortPercentage': 63, 'longPercentage': 37, 'shortVolume': 673.48, 'longVolume': 403.95, 'longPositions': 2123, 'shortPositions': 3785, 'totalPositions': 5908, 'avgShortPrice': 1.0644, 'avgLongPrice': 1.0779}]

as you can see the 'name': 'EURUSD' has multiple values like shortpercentage and longpercerntage , shortvolume and long volume etc etc

i am trying to get these shortpercentage and longpercentage for specific currencies and not all ,

like this :

EURUSD    32    68

GBPUSD    54    46

USDJPY    .......
:
:
:

i hope my question makes sense i tried my best to explain


Solution

  • Use comprehensation:

    # data = this is an input big data
    interested = ['EURUSD', 'GBPUSD']
    result = [{'pair' : pair['name'],
               'short' : pair['shortPercentage'],
               'long' : pair['longPercentage']}
              for pair in data if pair['name'] in interested]
    print(result)
    

    Output is shotred list:

    [{'pair': 'EURUSD', 'short': 32, 'long': 68}, {'pair': 'GBPUSD', 'short': 54, 'long': 46}]