uncommon

Json Remove key

4 posts in this topic

So I started using Json.au3 (2015.01.08) by Ward. Pretty good but I am having some real trouble figuring out how to remove a certain key.

There is an internal function called Json_ObjDelete which seems to use a Scripting.Dictionary Object this uses $Object.Remove($Key)

but this will only remove the first level key. For example if I use $Object.Remove('foo') in a json string like this...

{"foo":"foo","bar":["bar"],"test":["",{"foo":{"bar":["","",{"foo":{"bar":"Test"}}]}}]}

It will reduce do this...

{"bar":["bar"],"test":["",{"foo":{"bar":["","",{"foo":{"bar":"Test"}}]}}]}

So my question is how do I get to the next 'foo' and remove it because it does not seem to recognize others as a key?

Thank you for your time.


No problem can withstand the assault of sustained thinking.Voltaire

_Array2HTMLTable()_IEClassNameGetCollection()_IEquerySelectorAll()

Share this post


Link to post
Share on other sites



I'm not really familiar with json or that .au3, but could you do something hacky like:

Local $sJSON = '{"foo":"foo","bar":["bar"],"test":["",{"foo":{"bar":["","",{"foo":{"bar":"Test"}}]}}]}'
ConsoleWrite ( $sJSON & @CRLF )
$sJSON = StringRegExpReplace ( $sJSON, '"foo"[,:]', '' )
$sJSON = StringRegExpReplace ( $sJSON, '"foo"', '' )
ConsoleWrite ( "Deleted: " & $sJSON & @CRLF )

 

Share this post


Link to post
Share on other sites

That could work for this example but I need to be able to delete any part of if. There might be some 'foo' I want to remove and some I want to keep.

I am starting to think it might be a easier to use autoit as a wrapping in another language.


No problem can withstand the assault of sustained thinking.Voltaire

_Array2HTMLTable()_IEClassNameGetCollection()_IEquerySelectorAll()

Share this post


Link to post
Share on other sites

Figured it out. Use the key name in the object to get the next level object, if you do not know what will be in the next object and you need to iterate use the Keys function like this $Object.keys to return an array of the nested keys. When you get the final Parent object of the key you want to remove I used the builtin Json_ObjDelete function to remove the target key and value.

The Sample code below removes the bOpts Key and value. Hopefully this helps someone in the future.

Sample Code

$sJsonString = FileRead(@DesktopDir & "\codebeautify.json")
    $oJsonString = Json_Decode($sJsonString)
    $oItems = $oJsonString("Game")
    $oParts = $oItems("Interface")
    $oMenu = $oParts("MainMenu")
    $aMenu = $oMenu.keys
    _ArrayDisplay($aMenu)
    Json_ObjDelete($oMenu, "bOpts")

Sample Json

{
"Game":{
   "Name":"Game Title Goes Here",
   "Interface":{
      "MainMenu":{
         "bOpts":"Options",
         "bMp":"MultiPlayer",
         "bSp":"SinglePlayer",
         "bCreds":"Credits",
         "bQuit":"Quit Game"
      }
   }
   "Input":{
      "inMouse":"Mouse",
      "inJoy":"Joy Stick",
      "inKeys":"KeyBoard"
   }
}
"key2":"Key2 Text",
"key3":"Key3 Text",
"key4":"Key4 Text",
"num":1024,
"digits":{"1","2","3","4"}
}

 


No problem can withstand the assault of sustained thinking.Voltaire

_Array2HTMLTable()_IEClassNameGetCollection()_IEquerySelectorAll()

Share this post


Link to post
Share on other sites

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

  • Similar Content

    • baolo073
      By baolo073
      [
          [
              [
                  "Kinh Oanh,\r\n",
                  "Dear Oanh,\r\n",
                  null,
                  null,
                  3
              ],
              [
                  "C\u1ea3m \u01a1n b\u1ea1n r\u1ea5t nhi\u1ec1u v졢\u1ee9c th\u01b0 c\u1ee7a b\u1ea1n \u0111\u1ebfn m\u1ed9t v᩠ngṠtr\u01b0\u1edbc. ",
                  "Thank you very much for your letter which arrived a few days ago.",
                  null,
                  null,
                  3
              ],
              [
                  "Th\u1eadt \u0111⯧ y뵠khi nghe t\u1eeb b\u1ea1n.\r\n",
                  "It was lovely to hear from you.\r\n",
                  null,
                  null,
                  3
              ],
              [
                  "b\u1ea1n \u0111i \u0111㵠v\u1eady.\r\n",
                  "where do you go.\r\n",
                  null,
                  null,
                  1
              ],
              [
                  "T\u1ea1m bi\u1ec7t!",
                  "Goodbye!",
                  null,
                  null,
                  1
              ]
          ],
          null,
          "en",
          null,
          null,
          null,
          0.91366601,
          null,
          [
              [
                  "en"
              ],
              null,
              [
                  0.91366601
              ],
              [
                  "en"
              ]
          ]
      ]
      How to parse array to json?
    • NiftRex
      By NiftRex
      I'm trying to get an array from a website so that I can just get the url, but I am not sure how. I read a bit of arrays but I have a feeling I'd have to be writing a lot more than what I should be. I will include the script I have so far and the API url for what I want.
       
      API: https://api.fast.com/netflix/speedtest?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=1 (I want the 'url' array that contains the url)
       
      Code:
      #include <MsgBoxConstants.au3> #include <Inet.au3> #include <Array.au3> $site = _INetGetSource('http://api.fast.com/netflix/speedtest?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=1') MsgBox($MB_SYSTEMMODAL, "Title", $site[1])  
    • 31290
      By 31290
      Hi Guys, 
      Since I'm able to get a Dell equipment warranty status thanks to my API key, I'm using an UDF to extract data from an XML file and get the end date. > 
      Thing is, when using InetGet, the original file is in JSON format and the UDF is not working anymore, even if I download the file with the xml extension. Therefore, and when I manually download the page with Chrome, I have a proper XML file where the UDF is working fine.
      Here's my code:
      I even tried to convert the json to xml > https://www.autoitscript.com/forum/topic/185717-js-json-to-xml/
      I took a look here https://www.autoitscript.com/forum/topic/104150-json-udf-library-fully-rfc4627-compliant/ but I don't understand anything :/
       
      The XML read UDF is just perfect for my needs but I'm stuck here... 
      Thanks for any help you can provide
      -31290-
      3MTXM12.json
      3MTXM12.xml
    • cookiemonster
      By cookiemonster
      Im trying to read a JSON file into an array.
       
      The JSON looks like this:
       
      { "project_info": { "project_number": "123456789", "url": "https://www.website.com", "project_id": "PRJ08", "Bucket": "Buk09" }, "client": [ { "client_info": { "id": "1:1001306455", "info": { "name": "banana" } }, "oauth_client": [ { "client_id": "1001306455694-m3h6v", "client_type": 3 } ], "a_key": [ { "key": "dkldkdkd" } ], "Avail": { "ana": { "status": 1 }, "vit": { "status": 1, "other": [] }, "ad": { "status": 2 } } }, { "client_info": { "id": "1:1838346", "info": { "name": "orange" } }, "oauth_client": [ { "client_id": "2145696315633-dmdhe", "client_type": 3 } ], "a_key": [ { "key": "osikdme" } ], "Avail": { "ana": { "status": 1 }, "vit": { "status": 1, "other": [] }, "ad": { "status": 2 } } }, ], "configuration_version": "1" } What I want to do is read it all into one single large array, can anyone help? 
      All I have so far is:
      #include "JSON.au3" #include "array.au3" $file = fileread("C:\file.json") JsonToArray($file) Func JsonToArray($JSON) $JSON = StringRegExpReplace($JSON, "[\[\]{}]", "") $sBreak = StringSplit($JSON, ",") For $a = 1 To $sBreak[0] $t = _JSONDecode("{" & $sBreak[$a] & "}") _ArrayDisplay($t, "multi " & $a & " of " & $sBreak[0]) Next EndFunc ;==>JsonToArray  
    • ur
      By ur
      I have a github repository and when I run the below command it will retrieve a json file.
      curl --user "user:password" https://api.github.com/repos/ukreddy-erwin/Build-war/commits > Desktop/uday.html The output json file will be of the below format.
      [ { "sha": "525ddee5f25628f89c04980d7f683e9cd8f56b7a", "commit": { "author": { "name": "ukreddy", "email": "ukreddy@erwin.com", "date": "2016-12-26T12:27:14Z" }, "committer": { "name": "ukreddy", "email": "ukreddy@erwin.com", "date": "2016-12-26T12:27:14Z" }, "message": "changed gwt and gxt folder structure to build.xml", "tree": { "sha": "8d4f2cbca93efec18be8a17f25f78ad168e65347", "url": "https://api.github.com/repos/ukreddy-erwin/Build-war/git/trees/8d4f2cbca93efec18be8a17f25f78ad168e65347" }, "url": "https://api.github.com/repos/ukreddy-erwin/Build-war/git/commits/525ddee5f25628f89c04980d7f683e9cd8f56b7a", "comment_count": 0 }, "url": "https://api.github.com/repos/ukreddy-erwin/Build-war/commits/525ddee5f25628f89c04980d7f683e9cd8f56b7a", "html_url": "https://github.com/ukreddy-erwin/Build-war/commit/525ddee5f25628f89c04980d7f683e9cd8f56b7a", "comments_url": "https://api.github.com/repos/ukreddy-erwin/Build-war/commits/525ddee5f25628f89c04980d7f683e9cd8f56b7a/comments", "author": { "login": "ukreddy-erwin", "id": 21240709, "avatar_url": "https://avatars.githubusercontent.com/u/21240709?v=3", "gravatar_id": "", "url": "https://api.github.com/users/ukreddy-erwin", "html_url": "https://github.com/ukreddy-erwin", "followers_url": "https://api.github.com/users/ukreddy-erwin/followers", "following_url": "https://api.github.com/users/ukreddy-erwin/following{/other_user}", "gists_url": "https://api.github.com/users/ukreddy-erwin/gists{/gist_id}", "starred_url": "https://api.github.com/users/ukreddy-erwin/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/ukreddy-erwin/subscriptions", "organizations_url": "https://api.github.com/users/ukreddy-erwin/orgs", "repos_url": "https://api.github.com/users/ukreddy-erwin/repos", "events_url": "https://api.github.com/users/ukreddy-erwin/events{/privacy}", "received_events_url": "https://api.github.com/users/ukreddy-erwin/received_events", "type": "User", "site_admin": false }, "committer": { "login": "ukreddy-erwin", "id": 21240709, "avatar_url": "https://avatars.githubusercontent.com/u/21240709?v=3", "gravatar_id": "", "url": "https://api.github.com/users/ukreddy-erwin", "html_url": "https://github.com/ukreddy-erwin", "followers_url": "https://api.github.com/users/ukreddy-erwin/followers", "following_url": "https://api.github.com/users/ukreddy-erwin/following{/other_user}", "gists_url": "https://api.github.com/users/ukreddy-erwin/gists{/gist_id}", "starred_url": "https://api.github.com/users/ukreddy-erwin/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/ukreddy-erwin/subscriptions", "organizations_url": "https://api.github.com/users/ukreddy-erwin/orgs", "repos_url": "https://api.github.com/users/ukreddy-erwin/repos", "events_url": "https://api.github.com/users/ukreddy-erwin/events{/privacy}", "received_events_url": "https://api.github.com/users/ukreddy-erwin/received_events", "type": "User", "site_admin": false }, "parents": [ { "sha": "db4d61c5bbef555e6cc9f2e852aaf9d75772475c", "url": "https://api.github.com/repos/ukreddy-erwin/Build-war/commits/db4d61c5bbef555e6cc9f2e852aaf9d75772475c", "html_url": "https://github.com/ukreddy-erwin/Build-war/commit/db4d61c5bbef555e6cc9f2e852aaf9d75772475c" } ] } ] So, I need to parse above file and get the number of opening and closing brackets {} .
      So that would be the number of commits in the repository.
      Any suggestion on how to parse this file of json format.