0

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"

2
  • How are you getting the json? Commented Jul 25, 2016 at 5:48
  • 1
    Sorry I managed to figure it out. I just used a script to open the file and matched with regexp. I will edit in the code above. Commented Jul 25, 2016 at 6:53

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.