Kanat0x
Kanat0x

Reputation: 37

How to compare values within a json file [in Python]

i try to make an error prevention, in which I look if their is a double definition.

For example:

json1 = {
    "a": "python is good",
    "b": "i like java",
    "c": "python is good"
}

I want to iterate over the values and find out if I got the same value at another key.

To get a message like: "ERROR: double definition"

Is their a good way to compare these?

Upvotes: 0

Views: 160

Answers (1)

Osadhi Virochana
Osadhi Virochana

Reputation: 1302

You can use length(len() function) to compare them.

len(set(json1.values())) == len(json1.values())

You can use this in print like this:

print('ERROR: double definition' if len(set(json1.values())) != len(json1.values()) else 'All good')

Upvotes: 1

Related Questions