Jump to content

Get data from json


jesus40
 Share

Recommended Posts

$jobid = json_Get($objJson, '["result"]').Keys[0]
$sys_id = json_get($object, '.result.RITM0911888.sys_id')
$number = json_get($object, '.result.RITM0911888.number')
$state = json_Get($objJson, '.result.RITM0911888.state')
ConsoleWrite('! $sys_id = ' & $sys_id & '  $number = ' & $number & @CRLF )
ConsoleWrite("jobID=" & $jobid & @CRLF)
ConsoleWrite("state=" & $state & @CRLF)
ConsoleWrite("name  = " & json_Get($objJson, '["result"][$jobid]["sys_id"]') & @CRLF)
 
 
This is the script. Please help
Link to comment
Share on other sites

We are getting below results for $priority1 = json_Get($objJson, '["result"][RITM0011988]["priority"]') ;
 
Please see below snippet
 
 
$objJson = json_Decode($strReturnedData2)
Json_Dump($objJson)
$jobid = json_Get($objJson, '["result"]').Keys[0]      ;  Result = RITM0011988
$priority = json_Get($objJson, '.result.RITM0911888.priority')  ; Empty Result
$priority1 = json_Get($objJson, '["result"][RITM0011988]["priority"]') ; Result= 4
ConsoleWrite("priority=" & $priority & @CRLF)
ConsoleWrite("priority=" & $priority1 & @CRLF)
 
Here, I want to get the result for below expression. Please help.. ( See that we want to pass RITM0011988 as a variable bcoz we may get multiple RITM in json object)
 
$priority = json_Get($objJson, '["result"]& $jobid &["priority"]')
Link to comment
Share on other sites

  • Developers

I can't test with that script as it doesn't contain the data! Just copy&paste the json data into a variable in the script so I can test your issue, as for me it worked fine as shown.

Jos

 

edit...  like this:

#include "json.au3"
$data = '{"result" :' & _
'   {"RITM0911888" : {' & _
'       "sys_id" : "887970961bac54103985a82bbc4bcb20",' & _
'       "number":"SPTASK5791324",' & _
'       "priority":"4",' & _
'       "opened_by":"687e3673378d8050de381d8643990e96",' & _
'       "opened_at":"2020-05-05 07:38:17",' & _
'       "short_description":"Global Onboarding Request",' & _
'       "employee_status":"new_User",' & _
'       "First_Name":"Kirk",' & _
'       "Middle_Name":"Patrick"' & _
'       }' & _
'   }' & _
'}"'

Json_Dump($data)

$object = json_decode($data)
$sys_id = json_get($object, '.result.RITM0911888.sys_id')
$number = json_get($object, '.result.RITM0911888.number')
ConsoleWrite('! $sys_id = ' & $sys_id & '  $number = ' & $number & @CRLF )

 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Hi,
 
we are making a rest api call to get  the request details as a json object, so we are using the same values to process the request. 
Bit confused on why do we need  to convert service json response to varible.
 
In the response , we get multiple RITM's in one single json request. We need to loop through the json response to retrieve each values ( of the Key-value pair..eg: sys_id, priority)
 
In end, we ought to pass the RITM0011988 as a variable dynamically.
 
Hope, its clear. Please help 
 
Link to comment
Share on other sites

  • Developers

I can only help you when you work with me. you have not provided me with anything yet that isn't working.
I do not care whether you provide a script and data file separate of a script with the json data in there, as long as I have something that I can run from SciTE and see what isn't working for you.

It really can't be this hard now can it?;)

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Hi Jos,

 

As requested , sending the code snippet. Just copy it into SciTe. It should work. 

 

Inline comments will help you. We are actually looking to retrieve the inner values of all the RITM's list from the json response.

 

Let me know  the feasible way of working with you synchronously to achieve this.

 

Thanks for your prolonged support and help. It means lot to us

 

#include <AutoItConstants.au3>
#include <StringConstants.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include<WinHttp.au3>
#include<WinHttpConstants.au3>
#include <String.au3>
#include <Inet.au3>
#include <json.au3>

;This is the JSON response we are receiving from REST Api
$strReturnedData2 ='{"result":{"RITM0011988":{"sys_id":"887970961bac54103985a82bbc4bcb20","number":"SCTASK5791324","state":"1","priority":"4","opened_by":"687e3673378d8050de381d8643990e96","opened_at":"2020-05-05 07:38:17","short_description":"Global Onboarding Request","employee_status":"new_User","Submit_requestor":"aiat automation","Hiring_Manager":"Ranjith","First_Name":"Sri","Middle_Name":"Kanth","Last_name":"A","employee_ID":"132345","start_date":"2020-05-06 07:35:44","end_date":"2020-05-18 07:35:49","employee_type":"Contractor","title_or_Role_Function":"Lead","Department_or_Project_Name":"CRS","Proposed_Office_Cubicle":"Banglore","Location":"India","group_name":"CRS"}}}'

$objJson = json_Decode($strReturnedData2)
Json_Dump($objJson) 

;We are fetching RITM value here
$jobid = json_Get($objJson, '["result"]').Keys[0]

;BAsed on RITM value , we have to fetch the inner values of the particular RITM  ( key-value pairs)

$priority = json_Get($objJson, '["result"] & [$jobid]& ["priority"]'); But this is not working aand we are looking for this to work

;This is working , we are able to fetch the desired key-value with hard-coded RITM0011988
$priority1 = json_Get($objJson, '["result"][RITM0011988]["priority"]') 


ConsoleWrite("priority=" & $priority & @CRLF)
ConsoleWrite("priority1=" & $priority1 & @CRLF)
ConsoleWrite("string = " & '["result"]& [$jobid] &["priority"]' & @crlf)
ConsoleWrite("string = " & '["result"][RITM0011988]["priority"]' & @crlf)


;By using .notations as below, we aren't able to get the values
$sysid = json_Get($objJson, '["result"][RITM0011988]["sys_id"]')
ConsoleWrite("Sysid=" & $sysid & @CRLF)
$number = json_Get($objJson, '["result"][RITM0011988]["number"]')
ConsoleWrite("Number=" & $number & @CRLF)
$sys_id = json_get($objJson, '.result.RITM0911888.sys_id')
$number = json_get($objJson, '.result.RITM0911888.number')
$state = json_Get($objJson, '.result.RITM0911888.state')
ConsoleWrite('! $sys_id = ' & $sys_id & '  $number = ' & $number & @CRLF )
ConsoleWrite("jobID=" & $jobid & @CRLF)
ConsoleWrite("state=" & $state & @CRLF)
ConsoleWrite("name  = " & json_Get($objJson, '["result"][$jobid]["sys_id"]') & @CRLF)
$starttime = json_Get($objJson, '["result"][$jobid]')
ConsoleWrite("start  = " & $starttime & @CRLF)

 

 

 

 
Link to comment
Share on other sites

Try this line :

$priority = json_Get($objJson, '["result"][' & $jobid & ']["priority"]')

or another way :

$sysid = json_get($objJson, '.result.' & $jobid & '.sys_id')

 

Edited by Nine
Link to comment
Share on other sites

  • Developers
10 minutes ago, Nine said:

My nick is Nine not Demain. 

Nitpick ... the OP was so close. ;) 

 

14 minutes ago, Ranjith789 said:

And a big thanks to Jos

You're welcome. ;) 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 1 year later...
On 5/8/2020 at 10:35 AM, Jos said:

Well that should be pretty simple and something like this: (demo_json13.htm contains your json data in this case)

#include "json.au3"
$data = FileRead("demo_json13.htm")
Json_Dump($data)

$object = json_decode($data)
$sys_id = json_get($object, '.result.RITM0911888.sys_id')
$number = json_get($object, '.result.RITM0911888.number')
ConsoleWrite('! $sys_id = ' & $sys_id & '  $number = ' & $number & @CRLF )

Jos :) 

Hello. I need to write from doc.txt "market_hash_name" and "price" of items with "status:2".

Can you help me?

doc.txt

Link to comment
Share on other sites

  • Developers

@Syzox,

Please:

  • Post what you have tried yourself that isn't working... not a quote of another post that is none related to your question!
  • Do not PM me to solicit for my help!  

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Func items_csgo()

   $read = IniRead ( @ScriptDir&"\doc.txt", "General", "Settings", "Info" )
   $data = _INetGetSource($read)
   $object = json_decode($data)
   $items = json_get($object, '.items')
Local $i = 0

While 1
    $status = json_get($object, '.items[' & $i & '].status')
    If @error Then ExitLoop
    if $status = "1" Then
         $market_hash_name = json_get($object, '.balances[' & $i & '].market_hash_name')
         $price = json_get($object, '.balances[' & $i & '].price')
         ConsoleWrite('Status:' & $status & ' Price:' & $price & ' Market_hash_name = ' & $market_hash_name & @CRLF) ;### Debug Console
    EndIf
    $i += 1
 WEnd

EndFunc

What's wrong?

Link to comment
Share on other sites

  • Developers

No idea as the script you posted does run, and I am not putting in effort to try and make it run, so please post something I can run,  using your posted data file, so it can be tested. 

and :What is that data used for?

 

 

 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

 

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <file.au3>
#include <Inet.au3>
#include <json.au3>



#Region ### START Koda GUI section ### Form=
FileInstall('background.jpg', @ScriptDir & '\background.jpg')
$Form1 = GUICreate("Test", 500, 300, 0, 0)
GUICtrlCreatePic(@ScriptDir& '\background.jpg', 0, 0, 500, 300)
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState()
$Button1 = GUICtrlCreateButton("Items", 30, 30, 100, 30)

GUISetState(@SW_SHOW)


#EndRegion ### END Koda GUI section ###


Opt("TrayOnEventMode", 1)
TraySetClick (16)
Opt("TrayMenuMode", 3)
TraySetOnEvent ( -7, "show_gui" )
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "exit_app")


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
         Case $GUI_EVENT_MINIMIZE
         GUISetState(@SW_HIDE, $Form1)


        Case $Button1
            items_csgo()



   EndSwitch
WEnd

Func items_csgo()

   $read = IniRead ( @ScriptDir&"\doc.txt", "General", "Settings", "Info" )
   $data = _INetGetSource($read)
   $object = json_decode($data)
   $items = json_get($object, '.items')
Local $i = 0

While 1
    $status = json_get($object, '.items[' & $i & '].status')
    If @error Then ExitLoop
    if $status = "1" Then
         $market_hash_name = json_get($object, '.items[' & $i & '].market_hash_name')
         $price = json_get($object, '.items[' & $i & '].price')
         ConsoleWrite('Status:' & $status & ' Price:' & $price & ' Market_hash_name = ' & $market_hash_name & @CRLF) ;### Debug Console
    EndIf
    $i += 1
 WEnd

EndFunc

 

doc.txt

Link to comment
Share on other sites

  • Developers

I see this is all very much GAME related, so please read our forum rules about game automation!
In the mean time your last posted script logic seems fine:

#include "json.au3"
$data = FileRead("doc.txt")
$object = json_decode($data)
$items = json_get($object, '.items')
Local $i = 0

While 1
    $status = json_get($object, '.items[' & $i & '].status')
    If @error Then ExitLoop
    If $status = "1" Then
        $market_hash_name = json_get($object, '.items[' & $i & '].market_hash_name')
        $price = json_get($object, '.items[' & $i & '].price')
        ConsoleWrite('Status:' & $status & ' Price:' & $price & ' Market_hash_name = ' & $market_hash_name & @CRLF)  ;### Debug Console
    EndIf
    $i += 1
WEnd

output:

Status:1 Price:57.9 Market_hash_name = Michael Syfers  | FBI Sniper
Status:1 Price:86.98 Market_hash_name = Rezan The Ready | Sabre
Status:1 Price:189 Market_hash_name = Glock-18 | Nuclear Garden (Battle-Scarred)

Your first posted snippet is wrong!  I am sure you can find the difference. ;) 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Jos locked this topic
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...