Answer the question
In order to leave comments, you need to log in
How to change the json structure of the response on the client?
There is a service that returns json via API in the following format:
{
"result": [
{
"Columns": [
{
"Title": "id"
},
{
"Title": "Name"
},
{
"Title": "mapScale"
},
{
"Title": "code"
}
],
"Data": [
[
"4",
"Краснодар",
"11",
"krasnodar"
],
[
"7",
"Анапа",
"14",
"anapa"
],
[
"14",
"Геленджик",
"14",
"gelendzhik"
],
[
"15",
"Новороссийск",
"10",
"novorossiysk"
],
[
"16",
"Ростов-на-Дону",
"12",
"rostov"
],
[
"17",
"Сочи",
"15",
"sochi"
],
[
"24",
"Афипский",
"10",
""
],
[
"67",
"Майкоп",
"10",
"maykop"
]
],
"totalRow": 255
}
]
}
{
"data": [
{
"id" : "187",
"Name": "Москва",
"mapScale": "4",
"code": "moscow"
},
{
...
},
"totalRow": 255
]
}
Answer the question
In order to leave comments, you need to log in
def f_json(json):
res = json["result"][0]
cols = [list(col.values())[0] for col in res["Columns"]]
return {"data": [dict(zip(cols, row)) for row in res["Data"]]}
As noted in the comments, this is done simply by a loop. I posted an example here , maybe it will help you figure it out.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question