Delete an entry in json file based on field "Name"
-
say i have the name of "Studio Lights" how do i delete the entry for "Studio Lights" by it's name in this json file?
{ "Device": [ { "IPA": "", "Icon": "fa_bell", "Name": "Studio Lights", "Status": "online", "Type": "Strip", "UVLights": "false" }, { "IPA": "newdevice", "Icon": "fa_bolt", "Name": "jaddajadda", "Status": "online", "Type": "Strip", "UVLights": "false" }, { "IPA": "newdevice", "Icon": "fa_bolt", "Name": "lyse.net", "Status": "online", "Type": "Strip", "UVLights": "false" } ] }
this is what i got so far
QFile file(_JsonFile); file.open(QIODevice::ReadOnly); QByteArray data = file.readAll(); file.close(); QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(data, &error); QJsonObject root = doc.object();
-
@JonB would it be better to rewrite the json like so
{ "Studio Lights": { "IPA": "studiolights", "Icon": "fa_bell", "Name": "Studio Lights", "Status": "online", "Type": "Strip", "UVLights": "false" }, "newdevice": { "IPA": "newdevice", "Icon": "fa_bolt", "Name": "jaddajadda", "Status": "online", "Type": "Strip", "UVLights": "false" }, "newdevice2": { "IPA": "newdevice2", "Icon": "fa_bolt", "Name": "lyse.net", "Status": "online", "Type": "Strip", "UVLights": "false" } }
-
@Kris-Revi given the reply you've got in this other post, could this issue be marked as solved as well?
-
root.remove("Studio Lights");
...does what you want. Just write the file back as you did in the other post.
-
@AxelVienna said in Delete an entry in json file based on field "Name":
root.remove("Studio Lights");
this did nothing.
code:
QFile file(save_path + data_folder + device_json); file.open(QIODevice::ReadOnly | QIODevice::Text); QByteArray data = file.readAll(); file.close(); QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(data, &error); QJsonObject root = doc.object(); root.remove("Thisisjustatest"); doc = QJsonDocument(root); file.open(QIODevice::WriteOnly | QIODevice::Text); file.write(doc.toJson()); file.close();
json file
{ "Device": [ { "IPA": "newdevice", "Icon": "fa_bell", "Name": "Studio Lights <3", "Status": "online", "Type": "Matrix", "UVLights": "false" }, { "IPA": "computerdesk", "Icon": "fa_battery_full", "Name": "Computer Desk", "Status": "online", "Type": "Strip", "UVLights": "false" }, { "IPA": "newdevice", "Icon": "fa_bold", "Name": "Thisisjustatest", "Status": "online", "Type": "Strip", "UVLights": "false" } ] }
-
@Kris-Revi Why do you post now different Json than what you posted at the beginning? This is confusing...
It should be clear that with this JSON you can't delete "Thisisjustatest" using root.remove("Thisisjustatest") as root does not have such a key... -
@Kris-Revi
In addition to @jsulm.In this thread and another similar of yours I answered that if you wish to delete an object which has some attribute, say
Name
, set to some value likeThisisjustatest
, then you must iterate/recurse to find that object, so that you can remove it. It does not matter how many times you ask, the answer is the same. -
@jsulm said in Delete an entry in json file based on field "Name":
Why do you post now different Json than what you posted at the beginning? This is confusing...
it is different because keys are ordered alphabetically in a object!
i care about the order so i changed the structruce of the json and used an array! -
@Kris-Revi said in Delete an entry in json file based on field "Name":
changed the structruce of the json and used an array!
Then you should also change your code which removes a key to use an array? Shouldn't you?
i care about the order
This will break as soon as you get json from somewhere else. There is no ordering (and not needed, there is not even an API to get keys ordered in any way)
-
@Christian-Ehrlicher said in Delete an entry in json file based on field "Name":
There is no ordering (and not needed, there is not even an API to get keys ordered in any way)
The OP has previously asked about this and I have discussed/advised him what he can do about his requirement for "order" in https://forum.qt.io/topic/130499/adding-to-json-file-without-deleting-the-content/10.
-
I think this topic should be marked solved. It is clear that to delete an object by its key, you have to know both the key and its position in the JSON tree. If you don’t, you have to recurse through the JSON tree until you find what you want to delete. If there is no way to know or find the object of desire, nothing can be done with it at all. Order, btw, does not matter here. This is the beauty of proper recursion.