Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: JSON Get Keyname Value List Node WinHttp.WinHttpRequest.5.1 MSScriptControl.ScriptControl JScript Eval
;========================================================================================================================================== ; ; Get JSON string from URL and use it for key=value search. ; ;------------------------------------------------------------------------------------------------------------------------------------------ ; Detlev Dalitz.20120419. ; Inspired by WinBatch user Jason Walls. ; Using JScript function "json2txt" from Patrick Fisher, pwfisher.com. ;========================================================================================================================================== ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfFileGetFromURL (strFilename, strURL) ; DebugTrace (22, "") ; Allow DebugTrace continuation (inherit the debug mode from the caller). If strURL == "" Then Return @FALSE If strFilename == "" Then Return @FALSE strResponseText = "" objHTTP = CreateObject ("WinHttp.WinHttpRequest.5.1") objHTTP.Open("GET", strURL, @FALSE) ErrorMode (@OFF) objHTTP.Send() ErrorMode (@CANCEL) intLastError = LastError () If !intLastError Then FilePut (strFilename, objHTTP.ResponseText) ; FilePutW (strFilename, objHTTP.ResponseText) objHTTP = 0 Return intLastError #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfGetJSONList (strJSONData) If strJSONData == "" Then Return "" objJSC = CreateObject ("MSScriptControl.ScriptControl") objJSC.Language = "JScript" objJSC.AddCode(: `function json2txt(obj,path){var txt='';for(var key in obj){if(obj.hasOwnProperty(key)){if('object'==typeof(obj[key])){txt+=json2txt(obj[key],path+(path?'.':'')+key);}else{txt+=path+'.'+key+'\t'+obj[key]+'\n';}}}return txt;}`) Return objJSC.Eval(: `json2txt(` : strJSONData : `,'')`) ; JS code from Patrick Fisher at "http://stackoverflow.com/questions/10221229/list-all-keys-and-values-of-json" ; 2012-04-19T03:48:24. #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ; Test. DirChange (DirScript ()) strFileJSON = "JSON.txt" strFileJSONList = "JSON.List.txt" ; http://gomashup.com/cms/usa_zipcode_json stateabrv = "WA" strURL = "http://gomashup.com/json.php?fds=geo/usa/zipcode/state/" : stateabrv ; : "&jsoncallback" ; Get JSON string into local text file. If FileExist (strFileJSON) == 0 intLastError = udfFileGetFromURL (strFileJSON, strURL) Terminate (!!intLastError, "Terminated.", "WB LastError = " : intLastError : @LF : "URL = " : strURL) EndIf ; Convert JSON string into a list of key=value entries and store the list to local text file. If FileExist (strFileJSONList) == 0 strJSONData = FileGet (strFileJSON) strJSONList = udfGetJSONList (strJSONData) intBytes = FilePut (strFileJSONList, StrReplace (strJSONList, @LF, @CRLF)) EndIf ; Get the JSON key=value entries into a dim-2 array. arrJSON = ArrayFileGetCSV (strFileJSONList, 0, @TAB) ; Apply some key=value searches. strJSONKey = "result.0.City" strJSONValue = "" intRow = Arraysearch (arrJSON, strJSONKey) If intRow != -1 Then strJSONValue = arrJSON[intRow, 1] Pause ("Demo|udfGetJSONValue", "Key = " : strJSONKey : @LF : "Value = " : strJSONValue) strJSONKey = "result.715.City" strJSONValue = "" intRow = Arraysearch (arrJSON, strJSONKey) If intRow != -1 Then strJSONValue = arrJSON[intRow, 1] Pause ("Demo|udfGetJSONValue", "Key = " : strJSONKey : @LF : "Value = " : strJSONValue) strJSONKey = "result.21.ZipClass" strJSONValue = "" intRow = Arraysearch (arrJSON, strJSONKey) If intRow != -1 Then strJSONValue = arrJSON[intRow, 1] Pause ("Demo|udfGetJSONValue", "Key = " : strJSONKey : @LF : "Value = " : strJSONValue) Exit ;------------------------------------------------------------------------------------------------------------------------------------------
Article ID: W17965
Filename: udfGetJSONList.txt
File Created: 2012:04:19:08:25:34
Last Updated: 2012:04:19:08:25:34