我目前有一个文件,其中包含多个不在列表中或不在逗号之间的json(有点像
{id: 1, attributes{height:1, length:2}}{id: 2, attributes{height:4, length:1}}
)。
我正在尝试使用boto3中的.put_item(Item = json.loads(...))将它们上传到dynamoDB,所以我需要以正确的格式获取它。我目前正在使用jsonFileReader = jsonFileReader.decode().replace('}{', '},{')
以正确的格式获取json,但是,这会使它们变成字符串(通过使用type函数确认),因此我无法对其进行for循环并将其传递给.put_item函数以这种方式。如何将其从字符串转换为列表,以便可以在for循环中传递它?
答案 0 :(得分:1)
这样对您有用吗?:
import json
# get file data
raw_data = open("multi_json.json", "rt").read()
# make it look like an array of objects
array_string = "[" + raw_data.replace("}{","},{") + "]"
# convert to object list with json
object_list = json.loads(array_string)
# then do whatever you want with your list of objects.