Forum Archive

Storing text from textview as JSON String

ramvee

Hi Friends,
I just want to take input from TextView Or Any Text Clip And Store It In As A JSON String, including newlines and tabs. Lot of scripts teach how to strip escape characters, i want the program to insert them automatically.

It is taking me a lot of time to insert \n and \t manually.
I ultimately want to store a bunch recipes in JSON file, as simple dictionary.
Thank you. Ram

dgelessus

json.dump can be used to write a list or dict into a JSON file, and json.load to read a JSON file. The json library automatically escapes and unespaces all strings when writing/reading, that way you don't need to do any of that by hand.

ramvee

Thank You @dgelessus ,
I have been sitting and editing JSON data files manually, inserting \n \b..
:)