I have a below json config file in a project.
"Jackets": [
{
"GreenHSV": [30, 150, 72, 80, 255, 255]
},
{
"OrangeHSV": [0, 150, 72, 25, 255, 255]
}
]
I have to get the each value of GreenHSV (30, 150, 72, 80, 255, 255) and OrangeHSV 0, 150, 72, 25, 255, 255 in python code, keeping in mind that in future another HSV can be added like below
{
"RedHSV": [0, 150, 72, 25, 255, 255]
}
so the code should be able to read RedHSV values as well without making any changes in the code. In above JSON, Jackets is basically a list which contains dict of list (if I am not wrong). To get each color HSV value, my approach is below:
for i in range(len(config["Jackets"])):
print(config["Jackets"][i])
hsv_dict = config["Jackets"][i]
print(hsv_dict.keys())
name = hsv_dict.keys()
hsv_color_name = hsv_dict[name]
print(hsv_color_name[0], hsv_color_name[1], hsv_color_name[2])
print(hsv_color_name[3], hsv_color_name[4], hsv_color_name[5])
My approach is to first put a for loop with the range of len of Jackets so that if in future, new color is added, it will loop 3 times.
After that I have extracted the hsv_dict which contains this value {'GreenHSV': [30, 150, 72, 80, 255, 255]}. Now at this point I though I should get the key name of the dict as it will be GreenHSV or any next color, so that I can extract its value which will be list ([30, 150, 72, 80, 255, 255]) and then I can get the list values easily. But looks like my approach is wrong.
Can anyone please guide me. Thanks