Jump to content
jantograaf

Retrieving useable data from JSON

Recommended Posts

Hi all,

I'm trying to create a script that runs a JSON-query and then can retrieve some variables out of the returned, decoded object using JSON.au3. I have tried some other examples on this forum, but I'm stuck at one point. My query works perfectly and gets loaded into the variable $data. Then, decoding $data to $object seems to work as well. But then I can't get the date-field out of this JSON-structure with my script.

The JSON-structure returned looks like this:

{
    "content": [
    {
        "id": "451ec583-8f27-4926-82a3-a2d85e57a110",
        "createdDate": "2018-08-08T08:40:57.449004Z",
        "updatedDate": "2018-08-08T08:40:57.449004Z",
        "lastOpenedDate": "2018-08-08T08:40:57.449004Z",
        "date": "2018-04-26T00:00:00",
        "description": "X-Ray Exam",
        "patient":
        {
            "id": "f857238a-c75d-4760-b8d1-8f50f8f9bbfa",
            "createdDate": "2018-08-08T08:40:37.623976Z",
            "updatedDate": "2018-08-08T08:40:37.623976Z",
            "lastOpenedDate": "2018-08-08T08:40:37.623976Z",
            "name": "Fuerstonia",
            "birthDate": "2014-06-08",
            "breed": "",
            "chip": "",
            "color": "",
            "damsire": "",
            "sire": "",
            "neutered": false,
            "orthancUuid": "",
            "sex": "U",
            "species": "Paard",
            "ueln": "De 431310762114",
            "pmsReference": "",
            "origin": ""
        },
        "type": "study",
        "accessionNumber": "KME201806960467",
        "instanceUid": null,
        "orthancUuid": "",
        "sent": false,
        "seriesCount": 0,
        "modalityType": "RX",
        "typeAndModality": "study RX",
        "client":
        {
            "id": "be627195-8458-4927-8446-f1ef37b917a4",
            "createdDate": "2018-08-08T08:40:31.433968Z",
            "updatedDate": "2018-08-08T08:40:31.433968Z",
            "lastOpenedDate": "2018-08-08T09:26:49.512298Z",
            "via": "",
            "extraInfo": "",
            "pmsReference": "",
            "contact":
            {
                "id": "406cc555-c491-4c29-b6bb-8d903f0e35a9",
                "createdDate": "2018-08-08T08:40:31.428968Z",
                "updatedDate": "2018-08-08T08:40:31.428968Z",
                "lastName": "Client 1",
                "firstName": "",
                "company": "",
                "email": "",
                "language": "nl",
                "phone": "",
                "address":
                {
                    "id": "6fc7703c-137a-4e0a-ba96-8c7f38f2044b",
                    "city": "",
                    "country": "",
                    "line": "",
                    "postalCode": ""
                }
            }
        }
    },

My script looks like this:

#RequireAdmin

#include <json.au3>
#include <inet.au3>
#include <File.au3>

;Create a handle to a logfile (will be created if it doesnt exist)
Global $logfile = FileOpen("C:\VSOL\VSTK\Logs\MigrationFix\MigrationFix.log",9)
FileWriteLine($logfile,"Start script")

;Create the URL with the JSON-query
$URL = "http://localhost:8080/v0/studies/?seriesCount=1"
;Catch the query output into a variable
$data = _INetGetSource($URL)
;Check if there is any data at all in this variable, if not, exit
If Not $data Then
  MsgBox(1,"Error","No answer. Server is probably not running.")
  Exit
EndIf

;For debugging purposes
FileWriteLine($logfile,$data)

;Decode the JSON_string into a useable object
$object = Json_Decode($data,1000)
If @error Then
  FileWriteLine($logfile,"Error decoding JSON")
  Exit
EndIf

Local $i = 0

;Start a loop to retrieve the study date of each study...
While 1
  $study_date = json_get($object,'[' & $i & '].date')
  If @error Then 
    FileWriteLine($logfile,"Study-Date retrieval error")
    ExitLoop
  EndIf
  $i = $i + 1
WEnd

;Close the logfile
FileWriteLine($logfile,"Stop script")
FileClose($logfile)

;Open the logfile for quick reference
ShellExecute("C:\VSOL\VSTK\Logs\MigrationFix\MigrationFix.log")

If believe it has something to do with my json_get($object...)-command. Anyone who can point me in the right direction?

Thanks in advance!

Kind regards

Edited by jantograaf
Pushed enter too soon :-)

Share this post


Link to post
Share on other sites

Lookup and use the json_dump() function, in the json.au3 UDF file, to get a better idea of the naming scheme used to reference individual items.
 

Spoiler

 

I think this will work:

json_get($object,'.content[' & $i & '].date')

Disclaimer:  The suggestion is untested.

 

 

Edited by TheXman

Share this post


Link to post
Share on other sites

I seem to be using a different JSON.au3, because my version (by Ward & zserge) doesn't include this json_dump()-function?

Could you point me to a newer/more correct version please? And thanks again for replying so quickly to my question.

Share this post


Link to post
Share on other sites

On a side note, in general, you may run into problems processing an array like this.  If you don't encounter an "Index out of bounds" exception, you may end up in an endless loop or worse depending on the function calls.  ;)

 

;Start a loop to retrieve the study date of each study...
While 1
  $study_date = json_get($object,'[' & $i & '].date')
  If @error Then 
    FileWriteLine($logfile,"Study-Date retrieval error")
    ExitLoop
  EndIf
  $i = $i + 1
WEnd

Below, in the hidden content, you will see an example that shows 2 different ways of processing your array.  One example uses a ForNext and the other uses a ForEach.  The ForEach would most likely be used only if you wanted to process all of the items in an array.  However, you could always set a condition in which you exited the loop. 

If you want to use an endless loop to process an array, then you should test to make sure the index is not out of bounds before referencing any array item.  i.e.  If $i < Ubounds($aSomeArray) Then <do process> Else ExitLoop.
 

Spoiler

 

#Include <json.au3> ;<-- Change this line to point to your UDF

example()
Func example()
    Const $kJSON_STRING = _
        '{' & _
        '   "content": [' & _
        '       {' & _
        '           "date": "2018-04-26T00:00:00",' & _
        '           "description": "X-Ray Exam"' & _
        '       },' & _
        '       {' & _
        '           "date": "2018-07-03T00:00:00",' & _
        '           "description": "EKG"' & _
        '       }' & _
        '   ]' & _
        '}'


    Local $oJson      = Json_Decode($kJSON_STRING)
    Local $iItemCount = 0

    ConsoleWrite('***  Array processed using ForNext  ***' & @CRLF)
    $iItemCount = UBound(Json_Get($oJson, '.content'))
    For $i = 0 To $iItemCount - 1
        ;Process "content" item
        ConsoleWrite( _
            StringFormat("Description: %s (%s)\r\n", _
                Json_Get($oJson, StringFormat(".content[%i].description", $i)), _
                Json_Get($oJson, StringFormat(".content[%i].date"       , $i)) _
            ) _
        )
    Next

    ConsoleWrite(@CRLF & '***  Array processed using ForEach  ***' & @CRLF)
    For $oContentItem in Json_Get($oJson, '.content')
        ;Process "content" item
        ConsoleWrite( _
            StringFormat("Description: %s (%s)\r\n", _
                Json_Get($oContentItem, ".description"), _
                Json_Get($oContentItem, ".date"       ) _
            ) _
        )
    Next
EndFunc

 

 

 

 

 

Edited by TheXman

Share this post


Link to post
Share on other sites

Hi @TheXman,

Sorry for the late reply, but indeed, I didn't notice the solutions proposed in the hidden contents. I only got time now to continue working on the project so I'll see what your suggestions will do, but I'm quite sure that they will help me make some big steps forward. 

The moving through the array was copy-pasted from another script where they demonstrated the usage of this json.au3, so I didn't quite write or check this myself, but you have a very valid point about the risk of running out of bounds or ending up in an endless loop. I'll integrate your solution or I'll try to use the Json_GetCount()-function which I believe also exists in the json.au3 you pointed me to.

Thanks a lot for your support! :-)

Kind regards,

Jan

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By FUD
      hello 
      please i need help 
      i need to open link in default browser only one windows without duplicate if i try to open the same link 
       
      thanks 
    • By YogendraAtluri
      Hi, 
      I am new to AutoIT scripting and I am still learning. I am trying to communicate with a Labview application that acts like a server. it basically takes commands from the client. But for some commands, it also send back some data. 
      When i am sending commands from my script, i can see that the labview is getting them. But i am not able to get anything back. I tried different code pieces that are available online in the forum.
      This is the working piece of code which i been using to send data.
      #cs This module is used to establish tcp connection with lab view #ce #include <File.au3> Func SendCmd($cmd) TCPStartup() Local $IpAddress="192.168.10.101" Local $Port="5353" $Labview = TCPConnect($IpAddress,$Port) If @error Then ConsoleWrite('!--> TCPConnect error number ( ' & @error & ' ).' & @CRLF) TCPCloseSocket($Labview) TCPShutdown() Exit EndIf TCPSend($Labview, $cmd & @CRLF) TCPCloseSocket($Labview) TCPShutdown() EndFunc SendCmd("wt42d")  
      This is slightly modified code to send and receive data, which is not working. I am not getting any response back
      SendCmd("galil") Func SendCmd($cmd) TCPStartup() Local $IpAddress="192.168.10.101" Local $Port="5353" $Labview = TCPConnect($IpAddress,$Port) If @error Then ConsoleWrite('!--> TCPConnect error number ( ' & @error & ' ).' & @CRLF) TCPCloseSocket($Labview) TCPShutdown() Exit EndIf TCPSend($Labview, $cmd & @CRLF) $ip = @IPAddress1 ;create listening socket $Listensocket = TCPListen($ip, $Port) ConsoleWrite("Listening to Socket - " & $Listensocket & @CRLF) If $Listensocket = -1 Then ConsoleWrite("Exiting..." & @CRLF) Exit EndIf ;Accept incoming clients and recieve info While 1 $connectedsocket = TCPAccept($Listensocket) ConsoleWrite("Connecting to Socket - " & $connectedsocket & "Error -" & @error & @CRLF) If $ConnectedSocket >= 0 Then $ip2 = TCPRecv($connectedsocket,1000000) EndIf WEnd TCPCloseSocket($connectedsocket) TCPCloseSocket($Labview) TCPShutdown() EndFunc I am not getting anything back. I am getting the following output in the console
      +>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop. Listening to Socket - 544 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 Connecting to Socket - -1Error -0 its going through that loop forever. i need to force stop it.
      But when i open putty and send the same command, i am getting response right away. 
      Can someone please help me with that.
      Thanks in advance
      Regards
      Yogendra
    • By Bhooshan
      I need to mute an ongoing call on Microsoft teams without activating the window. Teams allows us to mute using shortcut key Ctrl+Shift+m but only when the window is active.
      # Used set option as ongoing call can be with any person which leads to change in Title Name.
      AutoItSetOption ( "WinTitleMatchMode", 2 ) 
      # I am not clear with the control ID which will be good to use here and also the key combination of ^M       
      ControlSend ( "Microsoft Teams", "", "[CLASS:Intermediate D3D Window; INSTANCE:1]", "{ctrl down}")
       
      Can anyone help...!!! 
       

    • By Sayed
      Hi there, 
      I'm new in AutoIt forms and using AutoIt to automate desktop application (able to automate the application normally but facing issue when I've to re-run the application twice within the same script...so need help in this please)
      here is the steps then followed by the issue in a brief : 
      1- run application . 
      2- do some actions (click menus,activate windows,set texts..)
      3- close the application. 
      4- run the application again & access the same controls.
      5- open the same windows again (like step 2)
      6- perform some validations (by getting texts from some text boxes)
      7- close the application again (and repeat 1-7 for 15 times in average )
      The issue 
      * all controls are accessible in the first run and actions done successfully on controls (for steps 1-3) BUT from the second run of the application from step-4 it's able to set focus only the main application window.
      Note: only unique properties used to while mapping the controls. 
      Error that appear in the console :
      UIAWrappers.au3" (1673) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: $x = Int($t[1] + ($t[3] / 2)) $x = Int($t[1] + (^ ERROR  
      Simple spy code  of one of the controls that has this strange issue(menubar&view menu Item): 
      ;~ *** Standard code maintainable *** #include "UIAWrappers.au3" AutoItSetOption("MustDeclareVars", 1) _UIA_setVar("oP1","Title:=XXX;controltype:=UIA_WindowControlTypeId;class:=WindowsForms10.Window.8.app") ;main app form xxx _UIA_setVar("oP2","Title:=menuStrip1;controltype:=UIA_MenuBarControlTypeId;class:=WindowsForms10.Window.8.app") ;menuStrip1 ;~ $oUIElement=_UIA_getObjectByFindAll("View.mainwindow", "title:=View;ControlType:=UIA_MenuItemControlTypeId", $treescope_subtree) _UIA_setVar("oUIElement","Title:=View;controltype:=UIA_MenuItemControlTypeId;class:=") ;ControlType:=UIA_MenuItemControlTypeId;classname:=") ;~ Actions split away from logical/technical definition above can come from configfiles ;~_UIA_Action("oP1","highlight") _UIA_Action("oP1","setfocus") ;~_UIA_Action("oP2","highlight") _UIA_Action("oP2","setfocus") _UIA_action("oUIElement","highlight") ;~_UIA_action("oUIElement","click")  
       
    • By HenryJiu
      Hello!
      I have a question , about Mac and Autoit3.
      I want to convert my script to Mac App,but Aut2Exe just convert to EXE!
      So I asked if there was a way to convert scripts to MAC App
      My English isn't very good,so don't laugh please😃
×
×
  • Create New...