Jump to content

Recommended Posts

Posted

JSON variables with a period in variable names are not supported by the Json_Get function:

#include "Json.au3"

Local $Json = '{"@odata_count": 24,"@odata.count": 24,"@odata-count": 24}'

Json_Dump($Json)

Local $Data = Json_Decode($Json)
If @error Then
    ConsoleWrite("Json_Decode: @error = " & @error & @CRLF)
EndIf

Local $iCount = Json_Get($Data, ".@odata_count")
If @error Then
    ConsoleWrite("Json_Get (1): @error = " & @error & @CRLF)
Else
    ConsoleWrite("$iCount = " & $iCount & @CRLF)
EndIf

$iCount = Json_Get($Data, ".@odata.count")
If @error Then
    ConsoleWrite("Json_Get (2): @error = " & @error & @CRLF)
Else
    ConsoleWrite("$iCount = " & $iCount & @CRLF)
EndIf

$iCount = Json_Get($Data, ".@odata-count")
If @error Then
    ConsoleWrite("Json_Get (3): @error = " & @error & @CRLF)
Else
    ConsoleWrite("$iCount = " & $iCount & @CRLF)
EndIf

The second call of Json_Get results in @error being 1.
Unfortunately, the Open Data Protocol (OData) standard defines some variables that contain a dot in their name (e.g. "@odata.context", "@odata.count", "@odata.id" ...).

Posted (edited)

@DonChunior

Using your example, the correct syntax would be:

$iCount = Json_Get($Data, '."@odata.count"')  ;Using dot-notation

or 

$iCount = Json_Get($Data, '["@odata.count"]')  ;Using bracket-notation

If key names contain special characters, such as periods, the key names should be wrapped in double quotes.

Edited by TheXman
Posted
19 minutes ago, TheXman said:

@DonChunior

Using your example, the correct syntax would be:

$iCount = Json_Get($Data, '."@odata.count"')  ;Using dot-notation

or 

$iCount = Json_Get($Data, '["@odata.count"]')  ;Using bracket-notation

 

Hi @TheXman,

unfortunately that doesn't work for me. 😕
I'm still getting 1 as @error.

Posted (edited)

It works for me.  Below, is your updated script that I ran:

Spoiler
#include "Json.au3"

Global $Json = '{"@odata_count": 24,"@odata.count": 24,"@odata-count": 24}'

Json_Dump($Json)

Global $Data = Json_Decode($Json)
If @error Then
    ConsoleWrite("Json_Decode: @error = " & @error & @CRLF)
EndIf

Global $iCount = Json_Get($Data, ".@odata_count")
If @error Then
    ConsoleWrite("Json_Get (1): @error = " & @error & @CRLF)
Else
    ConsoleWrite("$iCount = " & $iCount & @CRLF)
EndIf

$iCount = Json_Get($Data, '."@odata.count"')
;~ $iCount = Json_Get($Data, '["@odata.count"]')
If @error Then
    ConsoleWrite("Json_Get (2): @error = " & @error & @CRLF)
Else
    ConsoleWrite("$iCount = " & $iCount & @CRLF)
EndIf

$iCount = Json_Get($Data, ".@odata-count")
If @error Then
    ConsoleWrite("Json_Get (3): @error = " & @error & @CRLF)
Else
    ConsoleWrite("$iCount = " & $iCount & @CRLF)
EndIf
Output

+-> .@odata_count  =24
+-> .@odata.count  =24
+-> .@odata-count  =24
$iCount = 24
$iCount = 24
$iCount = 24

 

 

Edited by TheXman
Posted (edited)

I used the most current version posted in the first post. ( _Json(2021.11.20).zip )

Edit:  See my post below to get a copy of the version that I used.

Edited by TheXman
Posted (edited)
3 hours ago, DonChunior said:

Have you modified (fixed) the UDF in comparison to the original of @Ward?

I apologize.  I just rechecked my version.  It looks like I did make a small change back in 2022 that fixed this (and other) issues.  I haven't used this UDF for processing JSON datasets in several years, so I must have forgotten about the modification.  Here is the version I used:

Json(2022.09.18).au3

Edited by TheXman

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