json file

Post your questions and help other users.

Moderator: Martin

Post Reply
elektroinside
Posts: 69
Joined: 14 Feb 2019 15:04

json file

Post by elektroinside » 23 Apr 2019 22:45

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!

User avatar
Desmanto
Posts: 2076
Joined: 21 Jul 2017 17:50

Re: json file

Post by Desmanto » 24 Apr 2019 17:46

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)
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), Resurrection Remix Official 7.0.2 20190829, Pie 9.0, Rooted.

elektroinside
Posts: 69
Joined: 14 Feb 2019 15:04

Re: json file

Post by elektroinside » 24 Apr 2019 18:33

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 :-)

Post Reply