Read Betfair JSON in C# into human readable form

Post Reply
footyman2
Posts: 12
Joined: Fri May 11, 2018 11:31 am

I'm at early stages with this, I also find this webpage useful, bit more readable. Let me know if you find it useful so far, you should get the gist if you have some programming knowledge.

Please ask if anything below seems scary. It really isn't.

http://jsonviewer.stack.hu/

If anyone has classes set up to deserialise to, do please let me know, please feel free to augment. Make it a bit more human friendly, plus not used dynamic objects in anger much as yet still playing.

1) Download the football sample, and unzip using 7zip, I think winzip works too.
2) Download Linqpad it is free - and awesome.
3) Download Newtonsoft dll. The below code needs a reference to it. In Linqpad press F4, click browse, and where you downloaded this to and add the dll, it is in a subfolder called Json110r2\Bin\net45\Newtonsoft.Json.dll
4) Paste the following into the window. It will complain, and ask you to add a reference. If not look on Linqpad website on how to do all that.
and press F5 to run.

This should output
Middlesbrough v Man City
28202626
Over/Under 0.5 Goals
30/04/2017 13:05:00




string[] file = File.ReadAllLines(@"C:\temp\football-basic-sample");

//read the first record
foreach(var line in file.Take(1))
{
dynamic stuff = JsonConvert.DeserializeObject(line);

Console.WriteLine(stuff.mc[0].marketDefinition.eventName);
Console.WriteLine(stuff.mc[0].marketDefinition.eventId);
Console.WriteLine(stuff.mc[0].marketDefinition.name);

Console.WriteLine(stuff.mc[0].marketDefinition.openDate);

}
footyman2
Posts: 12
Joined: Fri May 11, 2018 11:31 am

Ok this isn't pretty but I have run out of time for today.

The below lists all home win prices and the time. Might have bugs.

Change to run C# program if running in Linqpad.

void Main()
{
string[] file = File.ReadAllLines(@"C:\temp\football-basic-sample");

string homeWinID = string.Empty;

//read the first record
foreach(var line in file.Take(1))
{
dynamic stuff = JsonConvert.DeserializeObject(line);

int market = 2; // match odds

Console.WriteLine(stuff.mc[market].marketDefinition.marketType);
Console.WriteLine(stuff.mc[market].marketDefinition.eventName);
Console.WriteLine("Event id " + stuff.mc[market].marketDefinition.eventId);
Console.WriteLine("Market id " + stuff.mc[market].id);
homeWinID = stuff.mc[market].marketDefinition.runners[0].id;
Console.WriteLine("Home Win id " + stuff.mc[market].marketDefinition.runners[0].id);
Console.WriteLine("Away Win id " + stuff.mc[market].marketDefinition.runners[1].id);
Console.WriteLine("Draw id " + stuff.mc[market].marketDefinition.runners[2].id);
Console.WriteLine(stuff.mc[market].marketDefinition.name);

Console.WriteLine(stuff.mc[market].marketDefinition.openDate);

}

//read data lines
foreach(var line in file.Skip(1))
{
dynamic stuff = JsonConvert.DeserializeObject(line);

//Console.WriteLine(stuff);

if (line.Contains(homeWinID) && !line.Contains("marketDefinition"))
{
//string time = JavaTimeStampToDateTime(double.Parse(stuff.pt).ToString());

string javDt = stuff.pt.ToString();
DateTime dt = JavaTimeStampToDateTime(double.Parse(javDt));
string time = stuff.pt;
Console.WriteLine(dt.ToString() + "," + stuff.mc[0].rc[0].ltp);
}

}

}

// Define other methods and classes here
public static DateTime JavaTimeStampToDateTime( double javaTimeStamp )
{
// Java timestamp is milliseconds past epoch
System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddMilliseconds( javaTimeStamp ).ToLocalTime();
return dtDateTime;
}
dougkpga
Posts: 11
Joined: Wed Aug 22, 2018 4:12 pm

Great work, although I am new to this.

I got a spreadsheet made with macros that imports the csv files from football-data.co.uk
footyman2
Posts: 12
Joined: Fri May 11, 2018 11:31 am

Cheers doug, are you a programmer, i made a full c# program to read it, about as far as i got though. Set up classes for the JSON too.

The JSON files are about the only place i can find in play data, they used to do CSV, but no more....unless someone knows otherwise?

If there is anyone out there looking to help for mutual benefit, let me know.

I have another program on that, i loaded quite a lot of those into a database, and back tested a basic strategy, which is my main one right now, more simple than i thought really.

Just got to avoid betting on scottish tennis players though. (When he wins, he's British!). Sorry guys north of the border, just kidding.
neilovan
Posts: 13
Joined: Sun Nov 18, 2018 11:12 am

Hi All,

I downloaded some free soccer data from betfair, but it arrives in 597 folders, with files to be extracted in each folder. Is there a way to extract all the files in these 597 folders to one location?

Thanks,

Neil.
Post Reply

Return to “Betfair Exchange API”