Delete an entry in json file based on field "Name"
-
wrote on 21 Sept 2021, 18:34 last edited by
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();
-
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();
wrote on 21 Sept 2021, 18:44 last edited by@Kris-Revi
At a guess: useQJsonObject::find()
orQJsonObject::contains()
to locate theName
key, or just iterate, and then delete its parent if that's what you want to do. -
@Kris-Revi
At a guess: useQJsonObject::find()
orQJsonObject::contains()
to locate theName
key, or just iterate, and then delete its parent if that's what you want to do.wrote on 21 Sept 2021, 19:36 last edited by@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" } }
-
wrote on 21 Sept 2021, 20:40 last edited byThis post is deleted!
-
@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" } }
wrote on 21 Sept 2021, 20:57 last edited by@Kris-Revi
I'm not with you? That is different JSON, and it's not what you asked? -
@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" } }
wrote on 22 Sept 2021, 13:05 last edited by@Kris-Revi given the reply you've got in this other post, could this issue be marked as solved as well?
-
wrote on 22 Sept 2021, 13:11 last edited by
root.remove("Studio Lights");
...does what you want. Just write the file back as you did in the other post.
-
root.remove("Studio Lights");
...does what you want. Just write the file back as you did in the other post.
wrote on 24 Sept 2021, 11:34 last edited by@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" } ] }
-
@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... -
@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" } ] }
wrote on 24 Sept 2021, 11:52 last edited by JonB@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. -
@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...wrote on 24 Sept 2021, 12:55 last edited by@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! -
Lifetime Qt Championwrote on 24 Sept 2021, 13:24 last edited by Christian Ehrlicher
@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)
-
@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)
wrote on 24 Sept 2021, 13:34 last edited by@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.
-
wrote on 26 Sept 2021, 08:36 last edited by AxelVienna
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.
6/14