zeehteam Posted November 14, 2021 Share Posted November 14, 2021 Hi, I am trying to get data from a json string, but I get no results when parsing [/script/droids] unless I remove the brackets, but I have to keep them. #include 'JSON.au3' Local $sJson = '{"project":"Listing droids","version":"1.0","author":{"name":"Luke","mail":"luke@2080.org"},"[/script/droids]":[{"name":"R2D2","type":"Astromecano","size":"0,96m"},{"name":"BB8","type":"Astromecano","size":"0,67m"},{"name":"C-3PO","type":"Social","size":"1,67m"}]}' Local $jsonObject = Json_Decode($sJson) Local $project = Json_Get($jsonObject, '.project') ; Listing droids Local $name = Json_Get($jsonObject, '.author.name') ; Luke Local $mail = Json_Get($jsonObject, '.author.mail') ; luke@2080.org MsgBox(0,'',$project&" - "&$name&" - "&$mail) Local $aData = Json_Get($jsonObject, '.[/script/droids]') Local $sName = Json_Get($jsonObject, '.[/script/droids][1].name') MsgBox(0,'',$sName&" and "&UBound($aData)) Link to comment Share on other sites More sharing options...
Danp2 Posted November 14, 2021 Share Posted November 14, 2021 This should work -- Local $aData = Json_ObjGet($jsonObject, '.[/script/droids]') Local $sName = Json_Get($aData[1], '.name') MsgBox(0,'',$sName&" and "&UBound($aData)) Zedna 1 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
zeehteam Posted November 14, 2021 Author Share Posted November 14, 2021 Thank you so much! It's working! Link to comment Share on other sites More sharing options...
zeehteam Posted November 15, 2021 Author Share Posted November 15, 2021 I am really lost with json.au3 UDF, now I can't encode the data back, [/script/droids] stuff does not appear. #include "Json.au3" Local $Obj Json_Put($Obj, '.project', 'Listing droids') Json_Put($Obj, '.version', '1.0') Json_Put($Obj, '.author.name', 'Luke') Json_Put($Obj, '.author.mail', 'luke@2080.org') Json_Put($Obj, '.[/script/droids][0].name', 'R2D2') Json_Put($Obj, '.[/script/droids][0]type', 'Astromecano') Json_Put($Obj, '.[/script/droids][0]size', '0,96m') Json_Put($Obj, '.[/script/droids][1].name', 'BB8') Json_Put($Obj, '.[/script/droids][1].type', 'Astromecano') Json_Put($Obj, '.[/script/droids][1].size', '0,67m') Json_Put($Obj, '.[/script/droids][2].name', 'C-3PO') Json_Put($Obj, '.[/script/droids][2].type', 'Social') Json_Put($Obj, '.[/script/droids][2].size', '1,67m') Local $Json = Json_Encode($Obj, $JSON_PRETTY_PRINT) MsgBox(0,'', $Json) Link to comment Share on other sites More sharing options...
TheXman Posted November 16, 2021 Share Posted November 16, 2021 (edited) { "project": "Listing droids", "version": "1.0", "author": { "name": "Luke", "mail": "luke@2080.org" }, "[/script/droids]": [ { "name": "R2D2", "type": "Astromecano", "size": "0,96m" }, { "name": "BB8", "type": "Astromecano", "size": "0,67m" }, { "name": "C-3PO", "type": "Social", "size": "1,67m" } ] } If you don't mind me asking, what application, API, or site created this JSON? Or, is this JSON created by one of your own applications? Although the JSON meets strictly-formatted standards, the key that includes surrounding brackets and slashes ("[/script/droids]") is (to put it tactfully) quite interesting. There are so many other ways it could have been done that would have kept the original intent. That particular key appears to be conveying more of a value than a key name. To help you understand your quandary as it relates to the JSON UDF, if you would have checked @error after your json_puts or json_gets, you would have seen that it failed with @error = 2. That means that the supplied notation was invalid. The JSON UDF, that was originally written by Ward and is currently maintained by @Jos, allows for 2 types of notation to identify the path of a value, dot-notation and bracket-notation. The current implementation of the json_get and json_put functions cannot handle keys that contain a leading bracket ("["). That's because it conflicts with how the functions identify normal bracket-notations. Unless or until @Jos, or someone else, modifies the JSON UDF to handle such keys, you will need to find alternative ways to handle JSON datasets with keys that start with "[". There are other UDFs that can handle strictly-formatted JSON datasets, with keys like the one you've provided. There are also non JSON-related functions that can probably get the job done as well. Of course, if you are the one creating that JSON, then I would suggest not having such keys. Then, your problem with the JSON UDF would no longer be a problem. Edited November 17, 2021 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 Link to comment Share on other sites More sharing options...
zeehteam Posted November 17, 2021 Author Share Posted November 17, 2021 Thank you, TheXman. I will follow your advice and replace the square brackets, maybe with < and >. Actually, I am converting a binary file to Json format, and some strings inside that file are between square brackets. Link to comment Share on other sites More sharing options...
TheXman Posted November 18, 2021 Share Posted November 18, 2021 As you can see below, the latest version of the json UDF (which was just posted) can correctly handle keys with embedded brackets. 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 Link to comment Share on other sites More sharing options...
zeehteam Posted December 1, 2021 Author Share Posted December 1, 2021 Sorry for the very late reply Thank you so much! Maybe I can try it again. Link to comment Share on other sites More sharing options...
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