Answer the question
In order to leave comments, you need to log in
How to convert a two-dimensional json array (?) into a one-dimensional one?
Hello everyone, I'm asking for help in solving a problem in python, I threw in a code that takes the first element of each nested array :(
[
{
"name": "spotify",
"data": [
{
"name": "Снова я напиваюсь",
"author": "SLAVA MARLOW1",
"parent": {
"position": 2,
"service": "spotify"
}
}
]
},
{
"name": "yandex",
"data": [
{
"name": "А если это любовь?",
"author": "HammAli & Navai_1",
"parent": {
"position": 34,
"service": "yandex"
}
},
{
"name": "Снова я напиваюсь",
"author": "SLAVA MARLOW_2",
"parent": {
"position": 29,
"service": "yandex"
}
}
]
},
{
"name": "apple",
"data": [
{
"name": "Снова я напиваюсь",
"author": "SLAVA MARLOW_3",
"parent": {
"position": 11,
"service": "apple"
}
},
{
"name": "А если это любовь?",
"author": "HammAli & Navai_2",
"parent": {
"position": 47,
"service": "apple"
}
}
]
}
]
[
[
{
"name": "Снова я напиваюсь",
"author": "SLAVA MARLOW",
"parent": {
"position": 2,
"service": "spotify"
}
},
{
"name": "Снова я напиваюсь",
"author": "SLAVA MARLOW",
"parent": {
"position": 29,
"service": "yandex"
}
},
{
"name": "Снова я напиваюсь",
"author": "SLAVA MARLOW",
"parent": {
"position": 11,
"service": "apple"
}
}
],
[
{
"name": "А если это любовь?",
"author": "HammAli & Navai",
"parent": {
"position": 47,
"service": "apple"
}
},
{
"name": "А если это любовь?",
"author": "HammAli & Navai",
"parent": {
"position": 34,
"service": "yandex"
}
}
]
]
import json
f = open('join.json')
data_array = json.load(f)
i = 0
massiv = []
for i in range(len(data_array)):
j = 0
dlina = len(data_array[i])
data_enter = data_array[i]
for j in range(len(data_enter)):
try:
data_enter = data_enter['data'][j]
massiv.append(data_enter['author'])
j+=1
except:
break
i+=1
print(massiv)
f.close()
Answer the question
In order to leave comments, you need to log in
Input data
Data to be output
import json
with open('join.json') as f:
original_data = json.load(f)
new_data = []
for element in original_data:
for data in element['data']:
name = data['name']
found = False
for new_element in new_data:
for new_list in new_data:
if new_list[0]['name'] == name:
new_list.append(data)
found = True
break
if found:
break
if not found:
new_data.append([data])
print(new_data)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question