@Kris-Revi said in adding to json file without deleting the content:
like what and how does that work?
All I meant was you'd have to make your saved JSON like, say,
{
"New Device": {
"IPA": "newdevice",
...,
"Order": 1
},
"Studio Lights": {
"IPA": "studiolights",
...,
"Order": 0
}
}
So I made each element have an Order attribute, counting from 0 upward like an array for the order they were in when I saved. Which as shown above happens randomly to have come out not in that order when saved. Now it's your responsibility when reloading and recreating "populate my device list with buttons for each device" to look at the Order attribute on each one and use that to order your device list as you want it to be.
The other way, if order is so important, is not to store as just an object with an arbitrary list of named attributes ( "New Device":, "Studio Lights": ...) as you have done here, precisely because you cannot order those. Instead store as an array/list like:
{
[
{
"Name": "New Device",
"IPA": "newdevice",
...,
"Order": 1
},
{
"Name": "Studio Lights",
"IPA": "studiolights",
...,
"Order": 0
}
]
}
This is an array/list of objects, so it does have an order which is preserved on save/restore. Of course, that then takes you back to your original code as to how to locate an object element by Name, but that's the price you pay if ordering is important to you.