Valve Data Format VDF to JSON

Valve stores a lot of its data for games in VDF format, its own internal data format that is very JSON-esque. I first ran into it parsing through the items_game.txt file for Dota 2.

So today I created a quick PHP script which will allow you to create valid JSON from VDF data, so you can parse it much more easily and hopefully make better use of it!


For those who would like to do this, but in a separate language here’s the series of regular expressions I applied, after first encapsulating the entire string in curly braces. NB: ${n} in the replacement is specifying a callback where n is the n’th matched item in the expression. There may be a different call back specification in your preferred language.

1
2
3
4
5
6
7
8
9
10
1) pattern: '/"([^"]*)"(\s*){/'
   replacement: '"${1}": {'
2) pattern: '/"([^"]*)"\s*"([^"]*)"/'
   replacement: '"${1}": "${2}",'
3) pattern: '/,(\s*[}\]])/'
   replacement: '${1}'
4) pattern: '/([}\]])(\s*)("[^"]*":\s*)?([{\[])/'
   replacement: '${1},${2}${3}${4}'
5) pattern: '/}(\s*"[^"]*":)/'
   replacement: '},${1}'

To the best of my knowledge these regular expressions cover all of VDF, but I cannot guarantee they will work forever. However if it ceases to work I will update it as soon as I notice/am informed.

Comments