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

    • jandings
      By jandings
      Hello there,
      since I spent some time to access the REST-API of the web shop system Shopware, I'd like to share a few lines to make life easier for others.
      With this you can access your Shopware database, reading and writing all kind of data. 
      Replies are JSON style.
      To work with JSON I usually depend on either just own AutoIt string operations or this AutoIt library:
      ; File        : Json.au3 (2015.01.08)
      ; Purpose    : A Non-Strict JavaScript Object Notation (JSON) Parser UDF
      ; Author    : Ward
      $UserName="xxxx" ;Shopware credentials of Shopware user who has the API checkbox ticked $PassWord="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;API-Key of this user, NOT the Password $URL="www.XXX.com/api/" $Command="orders/64682?useNumberAsId=true" ;A list of commands and options is available through shopware REST-API help; Here we read order number 64682 $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "https://" & $URL & $Command , False) $oHTTP.SetCredentials($UserName, $PassWord, 0) $oHTTP.Send() $response = $oHTTP.ResponseText ConsoleWrite ($response & @CRLF & @CRLF)  
    • BJP
      By BJP
      Hello.  I have a program that has used ADO database connection to return a query and then subsequently put the query results into an array using getrows.  See snippet below:
      $constrim="DRIVER={SQL Server};SERVER=xxx-xxxxx\CSC;DATABASE=xxxxxxxxx;uid=xxxxxxxxxx;pwd=xxxxxxxxxxxx;" $adCN = ObjCreate ("ADODB.Connection") ; <== Create SQL connection $adCN.Open ($constrim) ; local $sQuery = "select * from tbl_Apps"                     ; get all applications in the database local $oAppRecordSet = $adCN.Execute($sQuery) local $aAppsInDB = $oAppRecordSet.Getrows(5000) With the code above I can perform array operations very efficiently. 
      Now I need to get this data via JSON which is working (using Ward's JSON UDF)  but the data set returned is large (approx 4MB) and I'm wondering what is the most efficient way to get this data into an array.  
      Dim $obj = ObjCreate ("WinHttp.WinHttpRequest.5.1") $obj.Open("GET", $URL, false) $obj.SetRequestHeader("Content-Type", "application/json") $obj.Send() $json = JSON_decode( $obj.ResponseText ) Any help would be appreciated!!
    • 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