0

i am struggling with what should be very easy. I have got a List of dictionaries i get into my code via json.loads(). What i want is to append another dictionary to my list. My code looks like this:

import json
import copy

oldstringdata = "[{\"title\": \"test\", \"type\": \"text\"}]"
oldjsondata = json.loads(oldstringdata)
print(type(oldjsondata), oldjsondata, type(oldjsondata[0]), oldjsondata[0])

newstringdata = "{\"title\": \"test2\", \"type\": \"text2\"}"
newjsondata = json.loads(newstringdata)
print(type(newjsondata), newjsondata)

jsondata = oldjsondata.append(copy.deepcopy(newjsondata))
print(type(jsondata), jsondata)

stringdata = json.dumps(jsondata)
print(stringdata) 

My output is the following:

<class 'list'> [{'title': 'test', 'type': 'text'}] <class 'dict'> {'title': 'test', 'type': 'text'}
<class 'dict'> {'title': 'test2', 'type': 'text2'}
<class 'dict'> {'title': 'test2', 'type': 'text2'}
<class 'NoneType'> None
null

Process finished with exit code 0

But i would expect the output to be:

<class 'list'> [{'title': 'test', 'type': 'text'}] <class 'dict'> {'title': 'test', 'type': 'text'}
<class 'dict'> {'title': 'test2', 'type': 'text2'}
<class 'dict'> {'title': 'test2', 'type': 'text2'}
<class 'list'> [{'title': 'test', 'type': 'text'}, {'title': 'test2', 'type': 'text2'}]

Process finished with exit code 0

Thank you in advance for your answers!

HU4B4
  • 1
  • 2
  • 4
    you had an oopsie `jsondata = oldjsondata.append(copy.deepcopy(newjsondata))` `list.append` doesn't return anything, it modifies the list in place so just keep using `oldjsondata` – Nullman May 18 '22 at 08:46

0 Answers0