Home > Enterprise >  Mutilple JSON root element errors
Mutilple JSON root element errors

Time:08-13

The file I am calling is JSON with padding and I have done my simple coding to remove the padding but it appears by stringing together multiple JSON strings the formatting is not correct and I get root element errors.

I am using the output of the python program and running it through an online JSON formatter and validator website to check my output. I am a learner so please bear with my inexperience. All help appreciated.

import json
import re
import requests
        
payload = {}
headers = {}
     
        for race in range(1, 3):
            url = f"https://s3-ap-southeast-2.amazonaws.com/racevic.static/2018-01-01/flemington/sectionaltimes/race-{race}.json?callback=sectionaltimes_callback"
            response = requests.request("GET", url, headers=headers, data=payload)
          
            strip = 'sectionaltimes_callback'
            string = response.text
            repl =''
        
            result = re.sub(strip, repl, string)
            print(result)

CodePudding user response:

This is one way of obtaining the data you're looking for:

import requests
import json
import pandas as pd

      
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0',
       'Accept-Language' : 'en-US,en;q=0.5'}
for race in range(1, 3):
    url = f"https://s3-ap-southeast-2.amazonaws.com/racevic.static/2018-01-01/flemington/sectionaltimes/race-{race}.json?callback=sectionaltimes_callback"
    r = requests.get(url, headers=headers)
    json_obj = json.loads(r.text.split('sectionaltimes_callback(')[1].rsplit(')', 1)[0])
    df = pd.DataFrame(json_obj['Horses'])
    print(df)

This would return (print out in terminal) a dataframe for each race:

Comment FinalPosition   FinalPositionAbbreviation   FullName    SaddleNumber    HorseUrl    SilkUrl Trainer TrainerUrl  Jockey  ... DistanceVarToWinner SixHundredMetresTime    TwoHundredMetresTime    Early   Mid Late    OverallPeakSpeed    PeakSpeedLocation   OverallAvgSpeed DistanceFromRail
0   Resumes. Showed pace to lead well off the rail...   1   1st Crossing the Abbey  2   /horses/crossing-the-abbey  //s3-ap-southeast-2.amazonaws.com/racevic.silk...   T.Hughes    /trainers/tim-hughes    C.Williams  ...     32.84   11.43   57.4    68.2    65.3    68.9    400m    63.3    0.8
1   Same sire as Katy's Daughter out of dual stake...   2   2nd Khulaasa    5   /horses/khulaasa    //s3-ap-southeast-2.amazonaws.com/racevic.silk...   D. & B.Hayes & T.Dabernig   /trainers/david-hayes   D.Oliver    ... 0   32.61   11.29   56.6    68.4    66.0    69.2    700m    63.4    1.2
2   Trialled nicely before pleasing debut in what ...   3   3rd Graceful Star   4   /horses/graceful-star   //s3-ap-southeast-2.amazonaws.com/racevic.silk...   D. & B.Hayes & T.Dabernig   /trainers/david-hayes   A.Mallyon   ... 0   33.10   11.56   56.9    67.4    64.8    68.5    400m    62.8    4.4
3   Sat second at debut, hampered at the 700m then...   4   4th Carnina 1   /horses/carnina //s3-ap-southeast-2.amazonaws.com/racevic.silk...   T.Busuttin & N.Young    /trainers/trent-busuttin    B.Mertens   ...  1  33.30   11.80   56.9    68.2    63.9    68.9    400m    62.7    3.0
4   $75k yearling by a Magic Millions winner out o...   5   5th Mirette 7   /horses/mirette //s3-ap-southeast-2.amazonaws.com/racevic.silk...   A.Alexander /trainers/archie-alexander  J.Childs    ... 0   33.53   11.89   57.0    67.9    63.5    68.5    700m    62.5    3.8
5   $95k yearling by same sire as Pinot out of a s...   6   6th Dark Confidant  3   /horses/dark-confidant  //s3-ap-southeast-2.amazonaws.com/racevic.silk...   D. & B.Hayes & T.Dabernig   /trainers/david-hayes   D.Dunn  ...  2  33.74   11.91   56.4    67.1    63.3    68.8    700m    61.9    5.0
6   Same sire as Vega Magic out of imported stakes...   7   7th La Celestina    6   /horses/la-celestina    //s3-ap-southeast-2.amazonaws.com/racevic.silk...   D.R.Brideoake   /trainers/david-brideoake   D.M.Lane    ...  1  34.46   12.27   57.5    67.3    61.4    68.2    700m    61.7    0.8
7 rows × 29 columns

Comment FinalPosition   FinalPositionAbbreviation   FullName    SaddleNumber    HorseUrl    SilkUrl Trainer TrainerUrl  Jockey  ... DistanceVarToWinner SixHundredMetresTime    TwoHundredMetresTime    Early   Mid Late    OverallPeakSpeed    PeakSpeedLocation   OverallAvgSpeed DistanceFromRail
0   Game in defeat both runs this campaign. Better...   1   1st Wise Hero   2   /horses/wise-hero   //s3-ap-southeast-2.amazonaws.com/racevic.silk...   J.W.Price   /trainers/john-price    S.M.Thornton    ...     33.13   11.43   55.4    62.7    65.5    68.2    300m    61.7    0.7
1   Two runs since racing wide over this trip at C...   2   2nd Just Hifalutin  5   /horses/just-hifalutin  //s3-ap-southeast-2.amazonaws.com/racevic.silk...   E.Jusufovic /trainers/enver-jusufovic   L.Currie    ...  3  32.75   11.37   53.1    63.8    65.8    68.5    400m    61.7    3.3
2   Did a bit of early work at Seymour and was not...   3   3rd King Kohei  10  /horses/king-kohei  //s3-ap-southeast-2.amazonaws.com/racevic.silk...   Michael & Luke Cerchi   /trainers/mick-cerchi   
[...]
  • Related