Skip to content
Advertisement

How do I get a specific element from a json string?

I have a JSON string (not in a file): [{'x': 403.5, 'y': 53.0, 'width': 117, 'height': 106, 'class': 'fruitflies', 'confidence': 0.626}, {'x': 446.0, 'y': 189.0, 'width': 124, 'height': 130, 'class': 'fruitflies', 'confidence': 0.528}], now as you can see, the entire string is in a list. Inside the list is the dictionary. There are 2 dictionaries in the list and I want to get how many dictionaries there are (it’s only 2 for this example) and I want to get the 'confidence' values of both of them. They aren’t in a file, just in one line.

Advertisement

Answer

d_list = [{'x': 403.5, 'y': 53.0, 'width': 117, 'height': 106, 'class': 'fruitflies', 'confidence': 0.626}, {'x': 446.0, 'y': 189.0, 'width': 124, 'height': 130, 'class': 'fruitflies', 'confidence': 0.528}]
confidence_vals = [d['confidence'] for d in d_list]

Something like this?

Advertisement