JSON Atsakymai - robotautas/kursas GitHub Wiki

import json

with open("uzduotis.json", 'r') as file:
    data = json.load(file)

colors_list = []
for color in data['colors']:
    color_item = {
        "color": color['color'],
        "rgb": ", ".join(map(str, color['code']['rgba'][:-1])),
        "hex": color['code']['hex'],
    }
    colors_list.append(color_item)

colors = {"colors": colors_list}
print(colors)

with open("uzduotis_atlikta.json", 'w') as file:
    json.dump(colors, file)

Alternatyva:

import json

with open('uzduotis.json', 'r') as f:
    data = json.load(f)

color_list = []
for item in data['colors']:
    new_format = {}
    color = item['color']
    rgba_list = item['code']['rgba']
    rgb = ''.join(str(rgba_list))[1:-4]
    hexx = item['code']['hex']
    new_format.update({'color': color, 'rgb': rgb, 'hex': hexx})
    color_list.append(new_format)


new_dict = {'colors': color_list}
with open('uzduotis_ivykdyta.json', 'w') as file:
    json.dump(new_dict, file, indent=2)