Jump to content

Recommended Posts

  • Developers
Posted
  On 1/6/2023 at 8:46 AM, antai said:

(can I delete / edit my old posts to avoid this spam)?

Expand  

Soon you will when you are move out of the "New Members" group.

  On 1/6/2023 at 8:46 AM, antai said:

Json_ObjTo2DArray does not exists so it does not work anyway.

Expand  

Guess this is a statement about a conclusion you made for a challenge you have, rather than a question.... Maybe share that challenge as a question? ;) 

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.
  :)

Posted
  On 1/6/2023 at 10:24 AM, Jos said:

 

Guess this is a statement about a conclusion you made for a challenge you have, rather than a question.... Maybe share that challenge as a question? ;) 

Expand  

The challenge is to read a json file (basically excell table but in json) into a 2D array. Is it possible with this UDF?

There is this function at the first page of this thread:

Func getArrayFromJson($URI,$reqAuth,$post="")
    $result = Jsmn_Decode(goxRequest($URI,_Iif($reqAuth>0,getNonce()&$post,"")))
    $result = Jsmn_ObjTo2DArray($result)
    If (Not IsArray($result) Or UBound($result) = 0) Then
        MsgBox(0,"Error: "&$URI,$result)
        Return 0
    Else
        If(NOT IsArray($result[2][1])) Then
            MsgBox(0,$URI,$result[2][1])
            Return 0
        EndIf
    Return $result[2][1]
    EndIf
EndFunc

The code does not work for few errors, most notably that Json_ObjTo2DArray() is an undefined function.

Is the function custom made or is it somwhere hidden in the jsonau3?

  • 2 weeks later...
Posted (edited)

I've searched throughout this forum and the net for an easy way to extract JSON data from an existing file.  Changing the source file data format is not an option, but I managed to extract JSON data.  Using the extracted string as a test, I have been unsuccessful in retrieving data values.  I've tried different variations of the function-calling syntax, but have been unsuccessful. What am I doing wrong? 

Here is one of my attempts using the UDF posted by TheXman on 2021/11/20:

#include <Json.au3>
Local $TestString = 'headerName:"Item Title", field:"ItemTitle", sortable:true, width:259, checkboxSelection:true, headerCheckboxSelection:true'
Local $Obj = Json_Decode('{JsonItem:[{' & $TestString & '}]}')
Local $TestResult = Json_Get($Obj, '[JsonItem][headerName]')
 msgbox(0,"Test", "$TestResult = " & $TestResult)

Edited by BrunoJ
Posted
  On 1/21/2023 at 10:52 AM, BrunoJ said:

I've searched throughout this forum and the net for an easy way to extract JSON data from an existing file.  Changing the source file data format is not an option, but I managed to extract JSON data.  Using the extracted string as a test, I have been unsuccessful in retrieving data values.  I've tried different variations of the function-calling syntax, but have been unsuccessful. What am I doing wrong? 

Here is one of my attempts using the UDF posted by TheXman on 2021/11/20:

#include <Json.au3>
Local $TestString = 'headerName:"Item Title", field:"ItemTitle", sortable:true, width:259, checkboxSelection:true, headerCheckboxSelection:true'
Local $Obj = Json_Decode('{JsonItem:[{' & $TestString & '}]}')
Local $TestResult = Json_Get($Obj, '[JsonItem][headerName]')
 msgbox(0,"Test", "$TestResult = " & $TestResult)

Expand  

I've solved it by looking at more postings by people looking for help.  I can use dot notation to retrieve the values.  This works:

#include <Json.au3>

Local $TestString = 'headerName:"Item Title", field:"ItemTitle", sortable:true, width:259, checkboxSelection:true, headerCheckboxSelection:true'
local $Obj = Json_Decode('{' & $TestString & '}')
Local $TestResult = Json_Get($Obj, '.headerName')
 msgbox(0,"Test", "$TestResult = " & $TestResult)

  • 2 weeks later...
Posted

hi

i have been having a heck of a time trying to get the displayName from this json file.  the multi layer of the JSON is what i think is tricky - success?, result, participants (dont think success is a layer but have tried with and without it also.  since i am only trying to get all displayName entries, maybe stringregexp is better suited?  havent gotten that working either

Local $aArray = StringRegExp($sFileContents, '"displayName": [A-Za-z]', 3)
_ArrayDisplay($aArray)

any help would be GREATLY appreciated!

json snippet

  Quote

{
  "success": true,
  "result": {
    "participants": [
      {
        "id": "00d0ab00-00e0-0000-acfc-0d00d00b000b",
        "emailAddress": "Bob.Smith@xyz.com",
        "displayName": "Bob Smith (BS)",
        "firstName": "Bob",
        "lastName": "Smith"
      },
      {
        "id": "000ca000-0d0a-0000-00d0-0e0a0f000000",
        "emailAddress": "Jane.Jones@xyz.com",
        "displayName": "Jane Jones (JJ)",
        "firstName": "Jane",
        "lastName": "Jones"
      },

Expand  

 

#include "JSON.au3"

$json = @ScriptDir & "\response_1674826797651.json"

Local $sFileContents = FileRead($json)
Local $oJson = Json_Decode($sFileContents)

For $x = 0 To UBound($oJson) - 1
    $sDisplayName = Json_Get($oJson, '["result.participants"][' & $x & ']["displayName"]')

    ConsoleWrite($sDisplayName & @CRLF)
Next

 

Posted

The Json_Dump function can be used to assist with this type of issue. Here's an example --

#include "JSON.au3"

$json = '{' & _
'  "success": true,' & _
'  "result": {' & _
'    "participants": [' & _
'      {' & _
'        "id": "00d0ab00-00e0-0000-acfc-0d00d00b000b",' & _
'        "emailAddress": "Bob.Smith@xyz.com",' & _
'        "displayName": "Bob Smith (BS)",' & _
'        "firstName": "Bob",' & _
'        "lastName": "Smith"' & _
'      },' & _
'      {' & _
'        "id": "000ca000-0d0a-0000-00d0-0e0a0f000000",' & _
'        "emailAddress": "Jane.Jones@xyz.com",' & _
'        "displayName": "Jane Jones (JJ)",' & _
'        "firstName": "Jane",' & _
'        "lastName": "Jones"' & _
'      }' & _
'   ]' & _
'  }' & _
'}'

Json_Dump($json)

And the resulting output --

   +-> .success  =True
    +-> .result.participants[0].id  =00d0ab00-00e0-0000-acfc-0d00d00b000b
    +-> .result.participants[0].emailAddress  =Bob.Smith@xyz.com
    +-> .result.participants[0].displayName  =Bob Smith (BS)
    +-> .result.participants[0].firstName  =Bob
    +-> .result.participants[0].lastName  =Smith
    +-> .result.participants[1].id  =000ca000-0d0a-0000-00d0-0e0a0f000000
    +-> .result.participants[1].emailAddress  =Jane.Jones@xyz.com
    +-> .result.participants[1].displayName  =Jane Jones (JJ)
    +-> .result.participants[1].firstName  =Jane
    +-> .result.participants[1].lastName  =Jones

Using the above details, I would change your Json_Get line to this --

$sDisplayName = Json_Get($oJson, '.result.participants[' & $x & '].displayName')

 

Posted
  On 2/2/2023 at 6:31 PM, Danp2 said:

The Json_Dump function can be used to assist with this type of issue. Here's an example --

#include "JSON.au3"

$json = '{' & _
'  "success": true,' & _
'  "result": {' & _
'    "participants": [' & _
'      {' & _
'        "id": "00d0ab00-00e0-0000-acfc-0d00d00b000b",' & _
'        "emailAddress": "Bob.Smith@xyz.com",' & _
'        "displayName": "Bob Smith (BS)",' & _
'        "firstName": "Bob",' & _
'        "lastName": "Smith"' & _
'      },' & _
'      {' & _
'        "id": "000ca000-0d0a-0000-00d0-0e0a0f000000",' & _
'        "emailAddress": "Jane.Jones@xyz.com",' & _
'        "displayName": "Jane Jones (JJ)",' & _
'        "firstName": "Jane",' & _
'        "lastName": "Jones"' & _
'      }' & _
'   ]' & _
'  }' & _
'}'

Json_Dump($json)

And the resulting output --

   +-> .success  =True
    +-> .result.participants[0].id  =00d0ab00-00e0-0000-acfc-0d00d00b000b
    +-> .result.participants[0].emailAddress  =Bob.Smith@xyz.com
    +-> .result.participants[0].displayName  =Bob Smith (BS)
    +-> .result.participants[0].firstName  =Bob
    +-> .result.participants[0].lastName  =Smith
    +-> .result.participants[1].id  =000ca000-0d0a-0000-00d0-0e0a0f000000
    +-> .result.participants[1].emailAddress  =Jane.Jones@xyz.com
    +-> .result.participants[1].displayName  =Jane Jones (JJ)
    +-> .result.participants[1].firstName  =Jane
    +-> .result.participants[1].lastName  =Jones

Using the above details, I would change your Json_Get line to this --

$sDisplayName = Json_Get($oJson, '.result.participants[' & $x & '].displayName')

 

Expand  

thank you for your help!

i think i may be misunderstanding you.... because i am not getting the values for displayName 

#include "JSON.au3"

$json = '{' & _
'  "success": true,' & _
'  "result": {' & _
'    "participants": [' & _
'      {' & _
'        "id": "00d0ab00-00e0-0000-acfc-0d00d00b000b",' & _
'        "emailAddress": "Bob.Smith@xyz.com",' & _
'        "displayName": "Bob Smith (BS)",' & _
'        "firstName": "Bob",' & _
'        "lastName": "Smith"' & _
'      },' & _
'      {' & _
'        "id": "000ca000-0d0a-0000-00d0-0e0a0f000000",' & _
'        "emailAddress": "Jane.Jones@xyz.com",' & _
'        "displayName": "Jane Jones (JJ)",' & _
'        "firstName": "Jane",' & _
'        "lastName": "Jones"' & _
'      }' & _
'   ]' & _
'  }' & _
'}'

$json = Json_Dump($json)


For $x = 0 To UBound($json) - 1
    $sDisplayName = Json_Get($json, '.result.participants[' & $x & '].displayName')

    ConsoleWrite($sDisplayName & @CRLF) 
Next

 

Posted

Yes, you misunderstood. The Json_Dump usage is how you can determine the proper keys to retrieve the data, but you don't need it in the final script. Here's one way that you could do it --

#include "JSON.au3"

$json = '{' & _
'  "success": true,' & _
'  "result": {' & _
'    "participants": [' & _
'      {' & _
'        "id": "00d0ab00-00e0-0000-acfc-0d00d00b000b",' & _
'        "emailAddress": "Bob.Smith@xyz.com",' & _
'        "displayName": "Bob Smith (BS)",' & _
'        "firstName": "Bob",' & _
'        "lastName": "Smith"' & _
'      },' & _
'      {' & _
'        "id": "000ca000-0d0a-0000-00d0-0e0a0f000000",' & _
'        "emailAddress": "Jane.Jones@xyz.com",' & _
'        "displayName": "Jane Jones (JJ)",' & _
'        "firstName": "Jane",' & _
'        "lastName": "Jones"' & _
'      }' & _
'   ]' & _
'  }' & _
'}'

Local $oJson = Json_Decode($json)
Local $x = 0

While 1
    $sDisplayName = Json_Get($oJson, '.result.participants[' & $x & '].displayName')
    If @error Then ExitLoop

    ConsoleWrite($sDisplayName & @CRLF)
    $x += 1
WEnd

 

Posted
  On 2/2/2023 at 9:22 PM, Danp2 said:

Yes, you misunderstood. The Json_Dump usage is how you can determine the proper keys to retrieve the data, but you don't need it in the final script. Here's one way that you could do it --

#include "JSON.au3"

$json = '{' & _
'  "success": true,' & _
'  "result": {' & _
'    "participants": [' & _
'      {' & _
'        "id": "00d0ab00-00e0-0000-acfc-0d00d00b000b",' & _
'        "emailAddress": "Bob.Smith@xyz.com",' & _
'        "displayName": "Bob Smith (BS)",' & _
'        "firstName": "Bob",' & _
'        "lastName": "Smith"' & _
'      },' & _
'      {' & _
'        "id": "000ca000-0d0a-0000-00d0-0e0a0f000000",' & _
'        "emailAddress": "Jane.Jones@xyz.com",' & _
'        "displayName": "Jane Jones (JJ)",' & _
'        "firstName": "Jane",' & _
'        "lastName": "Jones"' & _
'      }' & _
'   ]' & _
'  }' & _
'}'

Local $oJson = Json_Decode($json)
Local $x = 0

While 1
    $sDisplayName = Json_Get($oJson, '.result.participants[' & $x & '].displayName')
    If @error Then ExitLoop

    ConsoleWrite($sDisplayName & @CRLF)
    $x += 1
WEnd

that worked!! thank you for clarifying.  i thought json_decode outputs an array?  im pretty sure ive used it that way before - with a for loop to process all the $x instances

Expand  

 

Posted
  On 2/2/2023 at 9:55 PM, gcue said:

i thought json_decode outputs an array? 

Expand  

json_decode returns the structure as it is mapped in the json-string.
If the outermost element in the json-string is an array definition, then json_decode returns an array.
On the other hand, if it is an object, then json-decode returns a Scripting.Dictionary object.
If the json-string is just a number, then json-decode just returns a number type.

The function json_decode maps the json-string into (nested) AutoIt data types.
Therefore, you must use the structure of the json-string as a guide when processing the output of json_decode.

Posted
  On 2/3/2023 at 5:44 AM, AspirinJunkie said:

json_decode returns the structure as it is mapped in the json-string.
If the outermost element in the json-string is an array definition, then json_decode returns an array.
On the other hand, if it is an object, then json-decode returns a Scripting.Dictionary object.
If the json-string is just a number, then json-decode just returns a number type.

The function json_decode maps the json-string into (nested) AutoIt data types.
Therefore, you must use the structure of the json-string as a guide when processing the output of json_decode.

Expand  

makes sense thank you for the info!

Posted (edited)

Hello world!

I'm able to get Primary Sme and Secondary Sme but I am not sure why I can ONLY get the first instance of Other SME and Business Owner but not subsequent ones???  I know sometimes Other Sme is missing so not sure how if that's what's throwing things off.  Help is GREATLY appreciated!

#include "json.au3"

$json = '[{"Overview":{"Classification":"Application","Status":"Standard","Id":"000012","Name":"AP","AlternateName":"AP","Description":"11/30/2023","ManufacturerOrVendor":"AP","Type":"SaaS","Location":"Cloud","ReviewCycle":"Semi-annually","CreatedBy":"04/19/2013 12:06 PM","LastModifiedBy":"AN - 09/06/2022 12:55 PM"},"Ownership":{"PrimarySme":"AndyNa","SecondarySme":"KevTu","OtherSmes":["Ross"],"BusinessOwners":["Sam"],"AccessOwner":"Sam","AccessControllers":["Sam","KevYo","Ross"],"ItgManagers":["Don"],"Ward":"Cady","PortfolioSteward":"Dunlow"},"ReviewableSystems":{"InternetFacing":"No","GeneratesAutomatedEmail":"No","Is13":"No","IsSC1":"No","IsSC2":"No","Support":"No","Recorded":"No"}},{"Overview":{"Classification":"Application","Status":"Standard","Id":"000013","Name":"AE","AlternateName":"AE System","Description":"anlysis/troubleshooting.","ManufacturerOrVendor":"Internal","Type":"Custom","Location":"AP","ReviewCycle":"Semi-annually","CreatedBy":"05/22/2013 10:37 AM","LastModifiedBy":"12/05/2022 09:31 AM"},"Ownership":{"PrimarySme":"Jan","SecondarySme":"Paul","OtherSmes":[],"BusinessOwners":["Mike"],"AccessOwner":"Mike","AccessControllers":["Bran","Steve"],"ItgManagers":["Paul"],"Steward":"Cai","PortfolioSteward":"Osman"},"ReviewableSystems":{"InternetFacing":"No","GeneratesAutomatedEmail":"No","Is1":"No","Is1":"No","Is2":"No","Support":"No","Recorded":"No"}},{"Overview":{"Classification":"Application","Status":"Standard","Id":"0000178","Name":"AF","AlternateName":"AF Service","Description":"intermediary","ManufacturerOrVendor":"Internal","Type":"Custom","Location":"G","ReviewCycle":"Semi-annually","CreatedBy":"09/20/2011 10:40 AM","LastModifiedBy":"09/05/2022 10:38 PM"},"Ownership":{"PrimarySme":"Subra","SecondarySme":"Kumar","OtherSmes":["Rony"],"BusinessOwners":["Rei"],"AccessOwner":"Rei","AccessControllers":["Jay","Al","Sky"],"ItgManagers":["Jay","Sky"],"Steward":"Rei","PortfolioSteward":"Clark"},"ReviewableSystems":{"InternetFacing":"No","GeneratesAutomatedEmail":"No","Is1":"No","Is1":"No","Is2":"No","Support":"Yes","Recorded":"No"}}]'

$ojson = Json_Decode($json)

For $x = 0 To UBound($oJson) - 1
    $primary_sme = Json_Get($oJson, "[" & $x & "][Ownership.PrimarySme]")
    $secondary_sme = Json_Get($oJson, "[" & $x & "][Ownership.SecondarySme]")
    $other_sme = Json_Get($oJson, "[" & $x & "][Ownership.OtherSmes][" & $x & "]")

    $business_owner = Json_Get($oJson, "[" & $x & "][Ownership.BusinessOwners][" & $x & "]")

    ConsoleWrite("Primary SME: " & $primary_sme & @CRLF)
    ConsoleWrite("Secondary SME: " & $secondary_sme & @CRLF)
    ConsoleWrite("Other SME: " & $other_sme & @CRLF)

    ConsoleWrite("Business Owner: " & $business_owner & @CRLF & @CRLF)
Next

 

Edited by gcue
Posted

OtherSmes is an array which in your example has only either 1 entry or none.
However, you pass $i as the index for this array - and this runs from 0 to 2.
Therefore, you only have a chance to get the value in the first loop pass where $i still has the value 0.
If you write it this way, however, you will get all the OtherSME from your example:

$other_sme = Json_Get($oJson, "[" & $x & "].Ownership.OtherSmes[0]")

I recommend you to visualize the JSON structure for yourself.
There are many online tools for that.
Then the structure becomes clearer.

  • 2 months later...
Posted

I have a question what options are for $Option in Json_StringEncode($String, $Option = 0)

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)
  On 5/5/2023 at 1:46 PM, mLipok said:

I have a question what options are for $Option in Json_StringEncode($String, $Option = 0)

Expand  

I saw this:

; The following constants can be combined to form options for Json_Encode()
Global Const $JSON_UNESCAPED_UNICODE = 1 ; Encode multibyte Unicode characters literally
Global Const $JSON_UNESCAPED_SLASHES = 2 ; Don't escape /
Global Const $JSON_HEX_TAG = 4 ; All < and > are converted to \u003C and \u003E
Global Const $JSON_HEX_AMP = 8 ; All &s are converted to \u0026
Global Const $JSON_HEX_APOS = 16 ; All ' are converted to \u0027
Global Const $JSON_HEX_QUOT = 32 ; All " are converted to \u0022
Global Const $JSON_UNESCAPED_ASCII = 64 ; Don't escape ascii charcters between chr(1) ~ chr(0x1f)
Global Const $JSON_PRETTY_PRINT = 128 ; Use whitespace in returned data to format it
Global Const $JSON_STRICT_PRINT = 256 ; Make sure returned JSON string is RFC4627 compliant
Global Const $JSON_UNQUOTED_STRING = 512 ; Output unquoted string if possible (conflicting with $Json_STRICT_PRINT)

Are $Option for Json_StringEncode() and Json_Encode() the same option values ?

 

Additionally:

I saw that @Danp2 uses 

Global Const $JSON_MLREFORMAT = 1024 ; Addition to constants from json.au3

https://github.com/Danp2/au3WebDriver/blob/6d50a0153f169bcf4aad9c6e74cc78c530b237e2/wd_core.au3#L91

In my opinion this variable should appear in JSON.au3 UDF and not in wd_core.au3 UDF

 

btw.
@Danp2 where or how did you find this new value ? Or is it just a combination of all mentioned above ?

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 5/6/2023 at 12:08 PM, mLipok said:

Are $Option for Json_StringEncode() and Json_Encode() the same option values ?

Expand  

Based on my testing, I can say Yes.

  Quote

In my opinion this variable should appear in JSON.au3 UDF and not in wd_core.au3 UDF

where or how did you find this new value ? Or is it just a combination of all mentioned above ?

Expand  

While this is certainly possible, it doesn't make sense to me because this constant isn't used or supported by the JSON UDF. I was simply using the next available bit to allow the optional removal of tabs and cr/lf in __WD_EscapeString. This could become an issue if the JSON UDF was ever expanded with additional options, but this would require the C source code that isn't included with the UDF.

The name of this constant can be changed if that is causing you grief. I could also use a higher bit to avoid possible future conflicts.

Posted (edited)
  On 5/6/2023 at 12:35 PM, Danp2 said:

but this would require the C source code that isn't included with the UDF.

Expand  

I'm not sure if it is it, but.... check this following links:
https://github.com/zserge/jsmn

https://zserge.com/jsmn/

from the UDF header:

; Source    : jsmn.c
; Author    : zserge
; Website   : http://zserge.com/jsmn.html

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

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
×
×
  • Create New...