Page 1 of 1

json file

Posted: 23 Apr 2019 22:45
by elektroinside
Is it possible to read a json file, look for a key, replace its value and (over)write only that new value for that particular key in the file?

Thanks!

Re: json file

Posted: 24 Apr 2019 17:46
by Desmanto
If you want only to replace text, just use replace() function and treat it like a usual text file replacement.

But if you need the specific json key-value replacement, you must convert it first to object, fromJSON(file_text), look for the key, assign new value and then convert it back to json. Automagic support indented function toJSON(file_text, true) or compact toJSON(file_text, false).

Other non destructive method, require specific replace() based on the key value you found and replace manually on the text variable, or probably using regex. Much more complicated. I should have a look on the json example first (censor out any sensitive/private data, if there is)

Re: json file

Posted: 24 Apr 2019 18:33
by elektroinside
Oh, very nice, thank you!
I'll try this probably tomorrow.
I'm trying to move part of the AM flows to the Raspberry Pie, control the lights in some particular and constant scenarios with some motion sensors and luminosity sensors combo, for which I don't really need AM all the way, to lower the load on my phone. And i need to write some config files on the pie with AM, occasionally.
That's why i need base64 and json :-)