DonChunior Posted Thursday at 01:39 PM Posted Thursday at 01:39 PM 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" ...).
TheXman Posted Thursday at 02:44 PM Posted Thursday at 02:44 PM (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 Thursday at 03:04 PM by TheXman argumentum 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
DonChunior Posted Thursday at 03:05 PM Posted Thursday at 03:05 PM 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.
TheXman Posted Thursday at 03:09 PM Posted Thursday at 03:09 PM (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 Thursday at 03:13 PM by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
DonChunior Posted Thursday at 03:13 PM Posted Thursday at 03:13 PM Still not for me. Have you modified (fixed) the UDF in comparison to the original of @Ward?
TheXman Posted Thursday at 03:15 PM Posted Thursday at 03:15 PM (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 Thursday at 04:19 PM by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
Developers Jos Posted Thursday at 03:27 PM Developers Posted Thursday at 03:27 PM There's a fix for dot notations in a couple of versions back. 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.
TheXman Posted Thursday at 03:30 PM Posted Thursday at 03:30 PM (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 Thursday at 07:13 PM by TheXman DonChunior 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now