Bolte
Bolte

Reputation: 141

VBScript - Parse Json Value & store as Variable

Ok guys so I need to obtain a value from a JSON file to be used inside a VBScript.

Here is the sample content:

{
"installedPacks": {
"vanilla": {
  "name": "vanilla",
  "build": "1.7.10",
  "directory": "%MODPACKS%\\vanilla"
}

I would like to read the contents of the file and locate specifically the build value (which in this case is 1.7.10) and assign it to a variable for later use.

I have an existing AppData variable that translates to:

objShell.ExpandEnvironmentStrings("%APPDATA%") & "\"

The file I need to open is in location: AppData & ".technic\installedPacks"

Upvotes: 0

Views: 993

Answers (1)

Bolte
Bolte

Reputation: 141

Here is the code I used.

Function ForgeJSON(strTxt)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile( AppData & "ModPacker\ForgeVer.json", 1)
installedPacks = objFile.ReadAll

Dim oRE
Dim colMatches
Dim oMatch, I
Set oRE = New Regexp
oRE.Global = True
oRE.Pattern = """build"":\s""(.+?)"""
oRE.IgnoreCase = False
Set colMatches = oRE.Execute(strTxt)
For Each oMatch In colMatches

    If oMatch.SubMatches(0) = "recommended" Then
    Else
        strNextmap = oMatch.SubMatches(0)
    End If

Next

If strNextmap = "" Or IsNull (strNextmap) Then
ParseJSON = "No Match Found"
Else
ParseJSON = strNextmap
End If
End Function

Upvotes: 1

Related Questions