Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse a complex json is vbscript

Status
Not open for further replies.

Disskyz

Programmer
Aug 29, 2018
7
US
I have never coded in vbscript. I need help parsing a json file...I need to store the username, password and accesslicensekey in variables. Here is a sample of the json file:
{"Security":{"UsernameToken":{"Username":"nelson1", "Password":"password"}, "UPSServiceAccessToken":{"AccessLicenseNumber":"FC0A24ABA65D5FD0"}}, "XAVRequest":{"Request":{"RequestOption":"1", "TransactionReference":{"CustomerContext":"Verify that an XAV request with a Transaction Reference Container returns a successful response."}}, "AddressValidationSource":"
", "MaximumCandidateListSize":"10", "AddressKeyFormat":{"ConsigneeName":"", "BuildingName":"", "AttentionName":"", AddressLine:["26601 ALISO CREEK ROAD", "STE D", "ALISO VIEJO TOWN CENTER"], "Region":"", "PoliticalDivision2":"ALISO VIEJO", "PoliticalDivision1":"CA", "PostcodePrimaryLow":"92656", "PostcodeExtendedLow":"1521", "Urbanization":"porto arundal", "CountryCode":"US"}}}


After the variables are stored, the entire security container needs to be removed from the file, and it rewritten. In this case, it would be rewritten as:
{"XAVRequest":{"Request":{"RequestOption":"1", "TransactionReference":{"CustomerContext":"Verify that an XAV request with a Transaction Reference Container returns a successful response."}}, "AddressValidationSource":"
", "MaximumCandidateListSize":"10", "AddressKeyFormat":{"ConsigneeName":"", "BuildingName":"", "AttentionName":"", AddressLine:["26601 ALISO CREEK ROAD", "STE D", "ALISO VIEJO TOWN CENTER"], "Region":"", "PoliticalDivision2":"ALISO VIEJO", "PoliticalDivision1":"CA", "PostcodePrimaryLow":"92656", "PostcodeExtendedLow":"1521", "Urbanization":"porto arundal", "CountryCode":"US"}}}
 
You might want to clean up your json file first, as your example isn't actually compliant ...
 
Thank you. Fixed the json:
{
"Security": {
"UsernameToken": {
"Username": "nelson1",
"Password": "password"
},
"UPSServiceAccessToken": {
"AccessLicenseNumber": "FC0A24ABA65D5FD0"
}
},
"XAVRequest": {
"Request": {
"RequestOption": "1",
"TransactionReference": {
"CustomerContext": "Verify that an XAV request with a Transaction Reference Container returns a successful response."
}
},
"AddressValidationSource": "",
"MaximumCandidateListSize ": "10 ",
"AddressKeyFormat ": {
"ConsigneeName ": "",
"BuildingName ": "",
"AttentionName ": "",
"AddressLine": ["26601 ALISO CREEK ROAD ", "STE D ", "ALISO VIEJO TOWN CENTER "],
"Region ": "",
"PoliticalDivision2 ": "ALISO VIEJO ",
"PoliticalDivision1 ": "CA ",
"PostcodePrimaryLow ": "92656 ",
"PostcodeExtendedLow ": "1521 ",
"Urbanization ": "porto arundal ",
"CountryCode ": "US"
}
}
}
 
Are you having problems parsing or storing or rewriting? What have you tried so far?
 
This is possibly not as complex as you think. We can break the back of it in about 4 or 5 lines of code.

In fact, since I had a quiet moment:

Code:
[blue]Sub JSONDecode(JSONString)
    With CreateObject("ScriptControl")
        .Language = "JScript"
        With .Eval("(" + JSONString + ")")
            Username = .Security.UsernameToken.Username
            Password = .Security.UsernameToken.Password
            License = .Security.UPSServiceAccessToken.AccessLicenseNumber
            NewJSON = "{""XAVRequest"":" & CreateObject("htmlfile").parentWindow.JSON.stringify(.XAVRequest) & "}"
        End With
    End With
    [green]' Username, Password, License and NewJSON now contain what you wanted.[/green]
End Sub[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top