Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert JSON into dict
#1
I want to add a string from a passed-in argument and insert it into a dict as a key-value.
The passed-in string is a piece of JSON, eg:
write_data('"body": {"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}', 1234)

def write_data(my_str, pageid):
    data = {
    'id' : str(pageid),
    <ADD_MY_STR HERE>
}
so it looks like
def write_data(my_str, pageid):
    data = {
    'id' : str(pageid),
    "body": {"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}
}
Reply
#2
What is the question?  Please include any relevant code (within the code blocks) and complete error reports (within the error blocks).  What were your results, and what did you expect the results to be.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
If it valid JSON then will json.loads(json_data) return a Python dictionary.
You update dictionary and can also dump i back to JSON with json.dumps(data).
Json doc.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a dict in dict cherry_cherry 4 62,667 Apr-08-2020, 12:25 PM
Last Post: perfringo
  read json string as dict goron 3 2,651 May-08-2019, 01:43 PM
Last Post: buran
  [split] How to insert JSON into MSSQL gehrenfeld 3 3,256 Jan-27-2019, 06:20 PM
Last Post: micseydel
  How to insert JSON into MSSQL raysefo 2 4,109 Jan-27-2019, 06:17 PM
Last Post: micseydel
  Insert using psycopg giving syntax error near "INSERT INTO" olgethorpe 4 15,497 Jul-21-2017, 07:39 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020