Jump to content

Json Remove key


uncommon
 Share

Recommended Posts

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()

Link to comment
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 )

 

Link to comment
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()

Link to comment
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()

Link to comment
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
 Share

×
×
  • Create New...