我有JSON数据以以下格式进入S3:
{\n \"data\": {\n \"event_type\": \"message.received\",\n \"id\": \"819\",\n \"occurred_at\": \"2020-10\",\n \"payload\": {\n \"cc\": [],\n \"completed_at\": null,\n \"cost\": null,\n \"direction\": \"inbound\",\n \"encoding\": \"GSM-7\",\n \"errors\": [],\n \"from\": {\n \"carrier\": \"Verizon\",\n \"line_type\": \"Wireless\",\n \"phone_number\": \"+111111111\"\n },\n \"id\": \"e8e0d1e3-dce3-\",\n \"media\": [],\n \"messaging_profile_id\": \"400176\",\n \"organization_id\": \"717d556f-ba4f-\",\n \"parts\": 1,\n \"received_at\": \"2020-1\",\n \"record_type\": \"message\",\n \"sent_at\": null,\n \"tags\": [],\n \"text\": \"Hi \",\n \"to\": [\n {\n \"carrier\": \"carr\",\n \"line_type\": \"Wireless\",\n \"phone_number\": \"+111111111\",\n}\n}"我想让它变成这样:
{
"data": {
"event_type": "message.received",
"id": "76a60230",
"occurred_at": "2020-12-1",
"payload": {
"cc": [],
"completed_at": null,
"cost": null,
"direction": "inbound",
"encoding": "GSM-7",
"errors": [],
"from": {
"carrier": "Verizon",
"line_type": "Wireless",
"phone_number": "+1111111111"
},
"id": "06c9c765",
"media": [],
"messaging_profile_id": "40017",
"organization_id": "717d5",
"parts": 1,
"received_at": "2020-1",
"record_type": "message",
"sent_at": null,
"tags": [],
"text": "Hi",
"to": [
{
"carrier": "abc",
"line_type": "Wireless",
"phone_number": "+1111111111",
"status": "delivered"
}
],
"type": "SMS",
"valid_until": null,
"failover_url": null,
"url": "https://639hpj"
},
"record_type": "event"
},
"meta": {
"attempt": 1,
"delivered_to": "https://639hpj"
}
}我保存的第一个JSON数据是行的,而不是Struct格式的。我没有保留实际的JSON数据,但它是以类似的格式保存的(但有效)。我想运行一个lambda函数,其中JSON数据不受\n和空格的限制。
上面的2JSON数据不是相同的,但是我将接收第一种类型的JSON数据,我想将它转换为第二种类型,它没有空格和\n。
发布于 2020-12-18 08:33:37
您意识到空格和换行符是print用于格式化的吗?
让我们将t称为您的第一个json (我在后面添加了缺少的括号来修正它):
t = '''{\n "data": {\n "event_type": "message.received",\n "id": "819",\n "occurred_at": "2020-10",\n "payload": {\n "cc": [],\n "completed_at": null,\n "cost": null,\n "direction": "inbound",\n "encoding": "GSM-7",\n "errors": [],\n "from": {\n "carrier": "Verizon",\n "line_type": "Wireless",\n "phone_number": "+111111111"\n },\n "id": "e8e0d1e3-dce3-",\n "media": [],\n "messaging_profile_id": "400176",\n "organization_id": "717d556f-ba4f-",\n "parts": 1,\n "received_at": "2020-1",\n "record_type": "message",\n "sent_at": null,\n "tags": [],\n "text": "Hi ",\n "to": [\n {\n "carrier": "carr",\n "line_type": "Wireless",\n "phone_number": "+111111111"\n}\n]\n}}}'''它印的是:
>>> print(t)
{
"data": {
"event_type": "message.received",
"id": "819",
"occurred_at": "2020-10",
"payload": {
"cc": [],
"completed_at": null,
"cost": null,
"direction": "inbound",
"encoding": "GSM-7",
"errors": [],
...要获得预期的表示形式,您应该:
js = json.loads(t)t2 = json.dumps(js)
t2实际上看起来像'{\n "data": {\n "event_type": "message.received",\n "id": "819",\n "occurred_at": "2020-10",\n "payload": {\n "cc": [],\n "completed_at": null,\n "cost": null,\n ...一艘班轮可以是:
print(json.dumps(json.loads(t), indent=2))发布于 2020-12-18 07:20:07
您可以首先将其加载为Python字典,其中包含:
import json
myDict = json.loads(jsonString)然后,将其转换回最小化/缩进的JSON字符串:
minimizedJSON = json.dumps(myDict)
indentedJSON = json.dumps(myDict, indent = <# of spaces>)发布于 2020-12-18 07:32:37
通常,这是通过评估完成的,它将把您的字符串解析为python字典。
import ast
# replace with full string of yours
s='{\n "data": {\n "event_type": "message.received",\n "id": "819"\n}\n}'
result = ast.literal_eval(s)
print(type(result), result)https://stackoverflow.com/questions/65353036
复制相似问题