Android Question Delete part of json file

Startup

Active Member
Licensed User
Longtime User
I know I can delete the whole file with "File.Delete(xui.defaultfolder, DataFile)". But how do I delete just part of this json file? I'm guessing there is some way to delete using the key of the value I want to delete?
 

DonManfred

Expert
Licensed User
Longtime User
read the json to a map/list. Remove the subpart of the containing map. Or parts of the list.
Rewrite a new json with the still available data.
 
Upvote 0

Startup

Active Member
Licensed User
Longtime User
read the json to a map/list. Remove the subpart of the containing map. Or parts of the list.
Rewrite a new json with the still available data.

OK This is what I wrote which works -

B4X:
Sub FileRemove(part As String)
    Dim xui As XUI, Data As Map
    Public const DataFile As String = "data.dat"
    Data.Initialize
    Dim ser As B4XSerializator
    Data = ser.ConvertBytesToObject(File.ReadBytes(xui.DefaultFolder, DataFile))
    Log(Data)
    Data.Remove(part)
    Log(Data)
    File.WriteBytes(xui.DefaultFolder, DataFile, ser.ConvertObjectToBytes(Data))
End Sub

Please let me know if you would change/improve. Thanks!
 
Upvote 0

Startup

Active Member
Licensed User
Longtime User
i would completely go another route.
I would use jsonparser to get a map/list out from the json, then i would remove unwanted keys from the map/list and generate a new json using jsongenerator.

Thanks, DonManfred! I incorrectly called the file a json file. It is a Data Map file (generated from a Preferences Dialog created with FormsBuilder built with a json file). Given this corrected description I would greatly appreciate any comment you might have on my code.
Thanks!
 
Upvote 0
Top