Xử lý dict/list lồng nhau ở dạng string

Em có một dict như thế này. Thực chất nó là những cái list và dict lồng nhau, nhưng mà khó chịu ở chỗ một số dict/list lồng bên trong lại được viết ở dạng string:

{"data":"[
	{
		\"ScheduleId\":\"8718142a-c6bd-4e71-9c33-590e1be8ede6\",
		\"NOC\":0,
		\"RegistrationId\":\"00000000-0000-0000-0000-000000000000\",
		\"Medal\":3,
		\"Record\":\"\",
		\"ScheduleInfo\":\"{
			\\\"EventId\\\":\\\"00000000-0000-0000-0000-000000000000\\\",
			\\\"CategoryId\\\":\\\"00000000-0000-0000-0000-000000000000\\\",
			\\\"Name\\\":\\\"Malaysia - Vietnam\\\",
			\\\"Description\\\":\\\"Lower Bracket Semi - Final\\\",
			\\\"StartTime\\\":\\\"2022-05-22T09:00:00\\\",
			\\\"EndTime\\\":\\\"2022-05-22T11:45:00\\\",
			\\\"PlaceId\\\":\\\"14f11f4e-b5ad-4666-bd1c-c780a5028ef8\\\",
			\\\"PlaceName\\\":\\\"Trung tâm Hội nghị Quốc gia\\\",
			\\\"StartingList\\\":\\\"[
				\\\\\\\"195fe76a-3d28-4d0e-b8c1-095890f59252\\\\\\\",
				\\\\\\\"88ee495c-2af6-47dc-b352-0c735538e021\\\\\\\",
				\\\\\\\"9d8fbfbb-f3c3-46db-a395-a6b3e29a4f1b\\\\\\\",
				\\\\\\\"94c96690-7ab1-4c86-8b76-b0911dc153ca\\\\\\\",
				\\\\\\\"28d2c889-f32e-4b7e-9255-36dd3fdd1497\\\\\\\",
				\\\\\\\"3ecc9463-e350-4851-842f-dec5818e4f05\\\\\\\",
				\\\\\\\"845d9dfb-2506-4564-8550-c4f808752d30\\\\\\\",
				\\\\\\\"9ab4b53a-0ade-4531-99fc-6df1a7098999\\\\\\\",
				\\\\\\\"1abe1fea-d2bd-4e3d-8c05-d837b39c34ca\\\\\\\",
				\\\\\\\"2da72008-1c91-47f0-934d-aa1eec8a4802\\\\\\\"
				]\\\",
			\\\"TotalRows\\\":0,
			\\\"ObjectType\\\":0,
			\\\"Id\\\":\\\"8718142a-c6bd-4e71-9c33-590e1be8ede6\\\",
			\\\"TenantId\\\":1076,
			\\\"WorkgroupId\\\":0,
			\\\"Status\\\":3,
			\\\"Active\\\":True,
			\\\"CreatedDate\\\":\\\"2022-05-22T08:59:47.477\\\",
			\\\"CreatedDateUnixTime\\\":1653209987,
			\\\"CreatedBy\\\":33825,
			\\\"CreatedByName\\\":\\\"esports\\\",
			\\\"LastEditedDate\\\":\\\"2022-05-22T13:11:32.877\\\",
			\\\"LastEditedDateUnixTime\\\":1653225092,
			\\\"LastEditedBy\\\":37218,
			\\\"LastEditedByName\\\":\\\"competition_admin\\\",
			\\\"ProfileData\\\":{
				\\\"Description_vi\\\":\\\"Chung kết nhánh thua\\\",
				\\\"Discipline\\\":\\\"\\\",
				\\\"Event\\\":\\\"Arena of Valor (Mobile) - Team Event\\\",
				\\\"EventId\\\":\\\"462\\\",
				\\\"Ext1\\\":\\\"BO5\\\",
				\\\"Ext2\\\":\\\"\\\",
				\\\"Ext3\\\":\\\"\\\",
				\\\"IndividualTeam\\\":\\\"[
					{
						\\\\\\\"Team\\\\\\\":0,
						\\\\\\\"TeamId\\\\\\\":1,
						\\\\\\\"RegistrationIds\\\\\\\":[
							\\\\\\\"195fe76a-3d28-4d0e-b8c1-095890f59252\\\\\\\",
							\\\\\\\"88ee495c-2af6-47dc-b352-0c735538e021\\\\\\\",
							\\\\\\\"9d8fbfbb-f3c3-46db-a395-a6b3e29a4f1b\\\\\\\",
							\\\\\\\"94c96690-7ab1-4c86-8b76-b0911dc153ca\\\\\\\",
							\\\\\\\"28d2c889-f32e-4b7e-9255-36dd3fdd1497\\\\\\\"
							]
						},
					{
						\\\\\\\"Team\\\\\\\":1,
						\\\\\\\"TeamId\\\\\\\":1,
						\\\\\\\"RegistrationIds\\\\\\\":[
							\\\\\\\"3ecc9463-e350-4851-842f-dec5818e4f05\\\\\\\",
							\\\\\\\"845d9dfb-2506-4564-8550-c4f808752d30\\\\\\\",
							\\\\\\\"9ab4b53a-0ade-4531-99fc-6df1a7098999\\\\\\\",
							\\\\\\\"1abe1fea-d2bd-4e3d-8c05-d837b39c34ca\\\\\\\",
							\\\\\\\"2da72008-1c91-47f0-934d-aa1eec8a4802\\\\\\\"
							]
						}
					]\\\",
				\\\"Name_vi\\\":\\\"Malaysia - Vietnam\\\",
				\\\"Noc1\\\":\\\"868\\\",
				\\\"Noc2\\\":\\\"858\\\",
				\\\"Sport\\\":\\\"eSports\\\",
				\\\"Results\\\":\\\"[
					{
						\\\\\\\"NOC\\\\\\\":\\\\\\\"868\\\\\\\",
						\\\\\\\"RegistrationId\\\\\\\":\\\\\\\"00000000-0000-0000-0000-000000000000\\\\\\\",
						\\\\\\\"Session\\\\\\\":\\\\\\\"{
							\\\\\\\\\\\\\\\"key\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"col1\\\\\\\\\\\\\\\",
							\\\\\\\\\\\\\\\"name\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"Chung cuộc\\\\\\\\\\\\\\\"
							}\\\\\\\",
						\\\\\\\"MatchId\\\\\\\":None,
						\\\\\\\"Result\\\\\\\":\\\\\\\"0\\\\\\\",
						\\\\\\\"IsPass\\\\\\\":False,
						\\\\\\\"Note\\\\\\\":\\\\\\\"0\\\\\\\",
						\\\\\\\"Medal\\\\\\\":0,
						\\\\\\\"Record\\\\\\\":\\\\\\\"\\\\\\\",
						\\\\\\\"Card1\\\\\\\":\\\\\\\"\\\\\\\",
						\\\\\\\"Card2\\\\\\\":\\\\\\\"\\\\\\\"
						},
					{
						\\\\\\\"NOC\\\\\\\":\\\\\\\"868\\\\\\\",
						\\\\\\\"RegistrationId\\\\\\\":\\\\\\\"00000000-0000-0000-0000-000000000000\\\\\\\",
						\\\\\\\"Session\\\\\\\":\\\\\\\"\\\\\\\",
						\\\\\\\"MatchId\\\\\\\":None,
						\\\\\\\"Result\\\\\\\":\\\\\\\"\\\\\\\",
						\\\\\\\"IsPass\\\\\\\":False,
						\\\\\\\"Note\\\\\\\":\\\\\\\"0\\\\\\\",
						\\\\\\\"Medal\\\\\\\":3,
						\\\\\\\"Record\\\\\\\":\\\\\\\"\\\\\\\",
						\\\\\\\"Card1\\\\\\\":\\\\\\\"\\\\\\\",
						\\\\\\\"Card2\\\\\\\":\\\\\\\"\\\\\\\"
						},
					]\\\",
				\\\"ResultsPhoto\\\":\\\"[]\\\"
				}
			}\"
		}
	]"
}

Em muốn xử lý mấy cái string đó để có thể truy cập dễ dàng hơn bằng code. Như thế này:

{"data":[
	{
		"ScheduleId":"8718142a-c6bd-4e71-9c33-590e1be8ede6",
		"NOC":0,
		"RegistrationId":"00000000-0000-0000-0000-000000000000",
		"Medal":3,
		"Record":"",
		"ScheduleInfo":{
			"EventId":"00000000-0000-0000-0000-000000000000",
			"CategoryId":"00000000-0000-0000-0000-000000000000",
			"Name":"Malaysia - Vietnam",
			"Description":"Lower Bracket Semi - Final",
			"StartTime":"2022-05-22T09:00:00",
			"EndTime":"2022-05-22T11:45:00",
			"PlaceId":"14f11f4e-b5ad-4666-bd1c-c780a5028ef8",
			"PlaceName":"Trung tâm Hội nghị Quốc gia",
			"StartingList":[
				"195fe76a-3d28-4d0e-b8c1-095890f59252",
				"88ee495c-2af6-47dc-b352-0c735538e021",
				"9d8fbfbb-f3c3-46db-a395-a6b3e29a4f1b",
				"94c96690-7ab1-4c86-8b76-b0911dc153ca",
				"28d2c889-f32e-4b7e-9255-36dd3fdd1497",
				"3ecc9463-e350-4851-842f-dec5818e4f05",
				"845d9dfb-2506-4564-8550-c4f808752d30",
				"9ab4b53a-0ade-4531-99fc-6df1a7098999",
				"1abe1fea-d2bd-4e3d-8c05-d837b39c34ca",
				"2da72008-1c91-47f0-934d-aa1eec8a4802"
				],
			"TotalRows":0,
			"ObjectType":0,
			"Id":"8718142a-c6bd-4e71-9c33-590e1be8ede6",
			"TenantId":1076,
			"WorkgroupId":0,
			"Status":3,
			"Active":True,
			"CreatedDate":"2022-05-22T08:59:47.477",
			"CreatedDateUnixTime":1653209987,
			"CreatedBy":33825,
			"CreatedByName":"esports",
			"LastEditedDate":"2022-05-22T13:11:32.877",
			"LastEditedDateUnixTime":1653225092,
			"LastEditedBy":37218,
			"LastEditedByName":"competition_admin",
			"ProfileData":{
				"Description_vi":"Chung kết nhánh thua",
				"Discipline":"",
				"Event":"Arena of Valor (Mobile) - Team Event",
				"EventId":"462",
				"Ext1":"BO5",
				"Ext2":"",
				"Ext3":"",
				"IndividualTeam":[
					{
						"Team":0,
						"TeamId":1,
						"RegistrationIds":[
							"195fe76a-3d28-4d0e-b8c1-095890f59252",
							"88ee495c-2af6-47dc-b352-0c735538e021",
							"9d8fbfbb-f3c3-46db-a395-a6b3e29a4f1b",
							"94c96690-7ab1-4c86-8b76-b0911dc153ca",
							"28d2c889-f32e-4b7e-9255-36dd3fdd1497"
							]
						},
					{
						"Team":1,
						"TeamId":1,
						"RegistrationIds":[
							"3ecc9463-e350-4851-842f-dec5818e4f05",
							"845d9dfb-2506-4564-8550-c4f808752d30",
							"9ab4b53a-0ade-4531-99fc-6df1a7098999",
							"1abe1fea-d2bd-4e3d-8c05-d837b39c34ca",
							"2da72008-1c91-47f0-934d-aa1eec8a4802"
							]
						}
					],
				"Name_vi":"Malaysia - Vietnam",
				"Noc1":"868",
				"Noc2":"858",
				"Sport":"eSports",
				"Results":[
					{
						"NOC":"868",
						"RegistrationId":"00000000-0000-0000-0000-000000000000",
						"Session":{
							"key":"col1",
							"name":"Chung cuộc"
							},
						"MatchId":None,
						"Result":"0",
						"IsPass":False,
						"Note":"0",
						"Medal":0,
						"Record":"",
						"Card1":"",
						"Card2":""
						},
					{
						"NOC":"868",
						"RegistrationId":"00000000-0000-0000-0000-000000000000",
						"Session":"",
						"MatchId":None,
						"Result":"",
						"IsPass":False,
						"Note":"0",
						"Medal":3,
						"Record":"",
						"Card1":"",
						"Card2":""
						},
					],
				"ResultsPhoto":[]
				}
			}
		}
	]
}

Nhờ các cao nhân giúp đỡ. Em xin cảm ơn.

Trước hết bạn thay thế r'\(\\+)"' thành '"' rồi gọi eval(string) là được :kissing:

1 Like

Dùng đệ quy, lặp các prop, prop nào string thì try parse

3 Likes

Bị re.error: unbalanced parenthesis at position 5 bác ơi

Í lộn, thay thế r'(\\+)"' thành '"' nhé bạn :kissing:

Bị SyntaxError: invalid syntax, nếu ko dùng eval mà in ra thì vẫn còn vài cái ngoặc kép nên sinh ra lỗi

    {"data":'[{
		"ScheduleId":"8718142a-c6bd-4e71-9c33-590e1be8ede6",
		"NOC":0,
		"RegistrationId":"00000000-0000-0000-0000-000000000000",
		"Medal":3,
		"Record":"",
		"ScheduleInfo":"{
			"EventId":"00000000-0000-0000-0000-000000000000",
			"CategoryId":"00000000-0000-0000-0000-000000000000",
			"Name":"Malaysia - Vietnam",
			"Description":"Lower Bracket Semi - Final",
			"StartTime":"2022-05-22T09:00:00",
			"EndTime":"2022-05-22T11:45:00",
			"PlaceId":"14f11f4e-b5ad-4666-bd1c-c780a5028ef8",
			"PlaceName":"Trung tâm Hội nghị Quốc gia",
			"StartingList":"[
				"195fe76a-3d28-4d0e-b8c1-095890f59252",
				"88ee495c-2af6-47dc-b352-0c735538e021",
				"9d8fbfbb-f3c3-46db-a395-a6b3e29a4f1b",
				"94c96690-7ab1-4c86-8b76-b0911dc153ca",
				"28d2c889-f32e-4b7e-9255-36dd3fdd1497",
				"3ecc9463-e350-4851-842f-dec5818e4f05",
				"845d9dfb-2506-4564-8550-c4f808752d30",
				"9ab4b53a-0ade-4531-99fc-6df1a7098999",
				"1abe1fea-d2bd-4e3d-8c05-d837b39c34ca",
				"2da72008-1c91-47f0-934d-aa1eec8a4802"
				]",
			"TotalRows":0,
			"ObjectType":0,
			"Id":"8718142a-c6bd-4e71-9c33-590e1be8ede6",
			"TenantId":1076,
			"WorkgroupId":0,
			"Status":3,
			"Active":True,
			"CreatedDate":"2022-05-22T08:59:47.477",
			"CreatedDateUnixTime":1653209987,
			"CreatedBy":33825,
			"CreatedByName":"esports",
			"LastEditedDate":"2022-05-22T13:11:32.877",
			"LastEditedDateUnixTime":1653225092,
			"LastEditedBy":37218,
			"LastEditedByName":"competition_admin",
			"ProfileData":{
				"Description_vi":"Chung kết nhánh thua",
				"Discipline":"","Event":"Arena of Valor (Mobile) - Team Event",
				"EventId":"462",
				"Ext1":"BO5",
				"Ext2":"",
				"Ext3":"",
				"IndividualTeam":"[
					{
						"Team":0,
						"TeamId":1,
						"RegistrationIds":[
							"195fe76a-3d28-4d0e-b8c1-095890f59252",
							"88ee495c-2af6-47dc-b352-0c735538e021",
							"9d8fbfbb-f3c3-46db-a395-a6b3e29a4f1b",
							"94c96690-7ab1-4c86-8b76-b0911dc153ca",
							"28d2c889-f32e-4b7e-9255-36dd3fdd1497"
							]
						},
					{
						"Team":1,
						"TeamId":1,
						"RegistrationIds":[
							"3ecc9463-e350-4851-842f-dec5818e4f05",
							"845d9dfb-2506-4564-8550-c4f808752d30",
							"9ab4b53a-0ade-4531-99fc-6df1a7098999",
							"1abe1fea-d2bd-4e3d-8c05-d837b39c34ca",
							"2da72008-1c91-47f0-934d-aa1eec8a4802"
							]
						}
					]",
				"Name_vi":"Malaysia - Vietnam",
				"Noc1":"868",
				"Noc2":"858",
				"Sport":"eSports",
				"Results":"[
					{
						"NOC":"868",
						"RegistrationId":"00000000-0000-0000-0000-000000000000",
						"Session":"{
							"key":"col1",
							"name":"Chung cuộc"
						}",
						"MatchId":None,
						"Result":"0",
						"IsPass":False,
						"Note":"0",
						"Medal":0,
						"Record":"",
						"Card1":"",
						"Card2":""
						},
					{
						"NOC":"868",
						"RegistrationId":"00000000-0000-0000-0000-000000000000",
						"Session":"",
						"MatchId":None,
						"Result":"",
						"IsPass":False,
						"Note":"0",
						"Medal":3,
						"Record":"",
						"Card1":"",
						"Card2":""
						},
					]",
				"ResultsPhoto":"[]"
				}
			}"
		}
	]'
}

Oops. bạn thử không replace gì mà eval nguyên cái string đó coi sao.

Giống cái ở trên đầu thôi bác, mà chỉ mất được 2 cái ngoặc kép ngoài cùng và mỗi cái giảm đc 1 \

Vấn đề là sau khi xử lý nó xong phải gắn ngược nó lại data cũ ấy bác. Em chưa có hướng đi.

Bạn có thể đệ quy xem. Mình viết qua đoạn code sơ lược nhất, bạn tự thêm thắt tiếp nhé.

import json

def your_parse(s):
    level_1_parsed_data = json.loads(s)

    if isinstance(level_1_parsed_data, list) or level_1_parsed_data là 1 string list:
        # nếu level_1_parsed_data là 1 array -> parse tiếp từng phần tử
        level_2_parsed_data = list(map(your_parse, level_1_parsed_data))
        return level_2_parsed_data

    if isinstance(level_1_parsed_data, dict) or level_1_parsed_data là 1 string dict:
        # nếu level_1_parsed_data là 1 dictionary -> parse tiếp từng value trong từng cặp (key, value)
        level_2_parsed_data = {key: your_parse(value) for key, value in level_1_parsed_data.items()}
        return level_2_parsed_data

    # nếu level_1_parsed_data không phải array hay dictionary
    return level_1_parsed_data
1 Like

Nếu bạn phải xử lý và trả lại cấu trúc gốc, thì phải bản nắm cấu trúc dữ liệu ngay từ đầu, thì chả cần đệ quy gì cả, field nào string có stringify, field nào string thật sự

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?