Following-up from a previous post Fetching NHL Play by Play game data where I created a Node.js app that enabled the fetching of NHL Play by Play JSON game files. The next step was to enable the parsing of JSON files into CSV files in order to do some further exploring… say like analyzing with R, load into a RDBMS, or visualizing it!

So I added a new javascript file convert.js to the existing nhlplaybyplay-node app on the GitHub repo:

One important thing! I’m using jq a lightweight and flexible command-line JSON processor. You can download it here or install using Homebrew by issuing the following command: brew install jq


Essentially the following code snippet from convert.js parses a JSON Play by Play file into a CSV file format. You can give it a try, assuming you have previously ran fetch.js and have JSON files(s) in your data folder. Simply replace ${season} and ${game} accordingly and copy to a Terminal session to execute.

*If you don’t have files, or have not installed the app, you can still try it out by downloading the following file and saving it as 2016020001.json the output after executing the below snippet will look like this 2016020001.csv

Snapshot image – Play by Play JSON for game 2016020001

Snapshot image – Converted 2016020001.json to 2016020001.csv

For further details on how to install and to use the app see the following
Excerpt: (This assumes you already ran fetch.js and have JSON files in your data/{season} folder)


Convert Play by Play JSON to CSV
node convert.js season [game]

To convert all games for a specific season
node fetch.js 20152016

To convert a specific game from a specific season
node convert.js 20162017 2016020001

Once all the JSON files as been converted to CSV, you can issue the following command to merge all of the CSV files into one (make sure to change your directory to where the files are located)

cat *.csv > 20152016.csv


Tagged with →  
  • Benoît

    Merci de partager tout cela! Le node fetch fonctionne très bien, mais j’ai un petit souci avec convert.js…un message d’erreur sort toujours :

    exec error: Error: Command failed: jq -r –arg season 20162017 –arg gameid 2016020001 ‘ as $p |[] | . as $c | [$season | tonumber, $gameid | tonumber] +
    [([$c.aoi[] | tostring] | join(“,”))] + [$p.awayteamid, $p.awayteamname, $p.awayteamnick, $p.hometeamid, $p.hometeamname, $p.hometeamnick] +
    [$, $c.asog, $c.desc, $c.eventid, $c.formalEventId] + [([$c.hoi[] | tostring] | join(“,”))] + [$c.hs, $c.hsog, $c.localtime, $c.p1name, $c.p2name,
    $c.p3name, $c.period, $, $c.pid1, $c.pid2, $c.pid3, $c.playername, $c.strength, $c.sweater, $c.teamid, $c.time, $c.type, $c.xcoord, $c.ycoord] |
    @csv’ ./data/20162017/2016020001.json > ./data/20162017/2016020001.csv
    ‘[]’ n’est pas reconnu en tant que commande interne
    ou externe, un programme ex?cutable ou un fichier de commandes.

    Je tourne un peu en rond, des conseils?