Jump to content
jantograaf

Retrieving useable data from JSON

Recommended Posts

jantograaf

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
TheXman

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
  • Thanks 1

Share this post


Link to post
Share on other sites
jantograaf

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
TheXman

 

  • Thanks 1

Share this post


Link to post
Share on other sites
TheXman

I'm not sure if you noticed, but if you click "reveal hidden contents", in my first post, I suggested a solution.

Edited by TheXman
  • Haha 1

Share this post


Link to post
Share on other sites
TheXman

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
  • Thanks 1

Share this post


Link to post
Share on other sites
jantograaf

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

  • Similar Content

    • gahhon
      By gahhon
      I have a python script automate.py and I wanna run it via CMD.
      As I know to execute python via CMD is something like this
      $CMD = "Something python here" Run(@ComSpec & " /c " & $CMD) But how can I just open the 1 CMD and send multiple commands to the CMD based on the function called?
      For instance like:

      Launch CMD and send python then send import automate If google() function is called - send automate.google() to the CMD If yahoo() function is called - send automate.yahoo() to the CMD  I don't want to execute multiple CMD to call the automate functions. This is due to inside the python script have global variable to handle duplicate flag.
      So if I execute multiple CMD to call the functions, the global variable will be keep new as NULL. Which is not my expectation.
       
      Thanks
    • gahhon
      By gahhon
      I only found that AutoIt can actually execute the entire python script or execute the .exe (converted to .exe from .py)
      But what if I just want call the particular function? If that possible to do so?
      This is because python can't create fancy GUI like AutoIT does. So my plan is use AutoIT to have fancy GUI.
      Then each buttons will trigger specific function in the python script or .exe file.
      For instances:
      btnSum - trigger function in python script or .exe file (funcSum)
      btnMinus - trigger function in python script or .exe file (funcMinus)
       
      Thanks for advance information.
    • nacerbaaziz
      By nacerbaaziz
      Hello my friends
      Can we create a single context menu on more than one item?
      For example, a context menu includes standardized options on more than one control, such as buttons or check boxes
      to Create a context menu on one item am using this function
      GUICtrlCreateContextMenu ($ HWND)
      How to link it with more than one element please?
      or if we can't do that, please give me a solution
      so i tried to add an context menus to all the controls but the script will be long, for that if their are any solutions i hope to give it to me
      thanks in advanced
    • jchd
      By jchd
      Here's a new version of my Dump.au3 file (dirty). I've recently added support to dissect (dump) as a treeview-like view any string in the variable conforming to a strict JSON grammar, whereever said string is: as a standalone string variable or in an array or a map. AFAICT the only relaxed constraint not implemented here is that numbers in parsed JSON may have leading zeroes, although leading zeroes are forbidden in pure JSON specifications. If required it wouldn't be hard to add this constraint.
      @c.haslam (among others) may find it useful. Use freely.
      Dump.au3
    • gahhon
      By gahhon
      _Metro_SplashTextScreen(1, "", $iMsg) AdlibRegister("_Metro_LoopInProgress") DirRemove($DIR_WA_FOLDER, 1) Local $iCopy = _WinAPI_ShellFileOperation($CUR_WA_FOLDER, $DIR_WA_FOLDER, $FO_COPY, BitOr($FOF_SIMPLEPROGRESS, $FOF_NOCONFIRMMKDIR)) AdlibUnRegister("_Metro_LoopInProgress") _Metro_SplashTextScree If @error Then _Metro_MsgBox(0, "", $iCopy) _FileWriteLog($LOG_INSTALLATION, "Error: " & $iCopy) Else _Metro_MsgBox(0, "", "Application is up to date!") _FileWriteLog($LOG_INSTALLATION, "Debug: Application is up to date.") EndIf According to the help file of _WinAPI_ShellFileOperation there are a lot of failure and error code. But how can I handle all of them and display corresponding error message based on the help file?
      The incident is like this:
      I'll lock the folder with permission, so when I execute the _WinAPI_ShellFileOperation to copy and overwrite the folder contents, it popped me an error message saying that permission issue to caused I can't access the folder and overwrite the contents.

      Then I clicked Cancel button, and my AutoIT application exit immediately and it's not execute the function _Metro_MsgBox(0, "", $iCopy) to display the message.
       
      Anyway, I did also tried the code below, the it still display "Application is up to date!"
      According to the help file, 120 - Security settings denied access to the source.
      If @error = 120 Then _Metro_MsgBox(0, "", $iCopy) _FileWriteLog($LOG_INSTALLATION, "Error: " & $iCopy) Else _Metro_MsgBox(0, "", "Application is up to date!") _FileWriteLog($LOG_INSTALLATION, "Debug: Application is up to date.") EndIf  
×