w3resource

Python JSON: Access only unique key value of a Python object

Python JSON: Exercise-9 with Solution

Write a Python program to access only unique key value of a Python object.

Sample Solution:-

Python Code:

import json
python_obj ='{"a":  1, "a":  2, "a":  3, "a": 4, "b": 1, "b": 2}'
print("Original Python object:")
print(python_obj)
json_obj = json.loads(python_obj)
print("\nUnique Key in a JSON object:")
print(json_obj) 

Output:

Original Python object:
{"a":  1, "a":  2, "a":  3, "a": 4, "b": 1, "b": 2}

Unique Key in a JSON object:
{'a': 4, 'b': 2}
 

Flowchart:

Flowchart: Access only unique key value of a Python object.

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to check whether a JSON string contains complex object or not.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz


Python: Tips of the Day

Python: Unknown Arguments Using *arguments

If your function can take in any number of arguments then add a * in front of the parameter name:

def myfunc(*arguments):
 for a in arguments:
   print a
myfunc(a)
myfunc(a,b)
myfunc(a,b,c)