<jupyter_start><jupyter_text>Q1. Write a Python program to convert JSON data to Python object.<jupyter_code>import json
json_obj =  '{ "Name":"David", "Class":"I", "Age":6 }'
python_obj = json.loads(json_obj)
print("\nJSON data:")
print(python_obj)
print("\nName: ",python_obj["Name"])
print("Class: ",python_obj["Class"])
print("Age: ",python_obj["Age"])<jupyter_output>JSON data:
{'Name': 'David', 'Class': 'I', 'Age': 6}

Name:  David
Class:  I
Age:  6<jupyter_text>Q2. Write a Python program to convert Python object to JSON data.<jupyter_code>import json
# a Python object (dict):
python_obj = {
  "name": "David",
  "class":"I",
  "age": 6  
}
print(type(python_obj))
# convert into JSON:
j_data = json.dumps(python_obj)

# result is a JSON string:
print(j_data)<jupyter_output><class 'dict'>
{"name": "David", "class": "I", "age": 6}<jupyter_text>Q3. Write a Python program to convert Python objects into JSON strings. Print all the values.<jupyter_code>import json
python_dict =  {"name": "David", "age": 6, "class":"I"}
python_list =  ["Red", "Green", "Black"]
python_str =  "Python Json"
python_int =  (1234)
python_float =  (21.34)
python_T =  (True)
python_F =  (False)
python_N =  (None)

json_dict = json.dumps(python_dict)
json_list = json.dumps(python_list)
json_str = json.dumps(python_str)
json_num1 = json.dumps(python_int)
json_num2 = json.dumps(python_float)
json_t = json.dumps(python_T)
json_f = json.dumps(python_F)
json_n = json.dumps(python_N)

print("json dict : ", json_dict)
print("jason list : ", json_list)
print("json string : ", json_str)
print("json number1 : ", json_num1)
print("json number2 : ", json_num2)
print("json true : ", json_t)
print("json false : ", json_f)
print("json null ; ", json_n)<jupyter_output>json dict :  {"name": "David", "age": 6, "class": "I"}
jason list :  ["Red", "Green", "Black"]
json string :  "Python Json"
json number1 :  1234
json number2 :  21.34
json true :  true
json false :  false
json null ;  null<jupyter_text>Q4. Write a Python program to convert Python dictionary object (sort by key) to JSON data. Print the object members with indent level 4.<jupyter_code>import json
j_str = {'4': 5, '6': 7, '1': 3, '2': 4}
print("Original String:")
print(j_str)
print("\nJSON data:")
print(json.dumps(j_str, sort_keys=True, indent=4))<jupyter_output>Original String:
{'4': 5, '6': 7, '1': 3, '2': 4}

JSON data:
{
    "1": 3,
    "2": 4,
    "4": 5,
    "6": 7
}<jupyter_text>Q5. Write a Python program to convert JSON encoded data into Python objects.<jupyter_code>import json

jobj_dict =  '{"name": "David", "age": 6, "class": "I"}'
jobj_list =   '["Red", "Green", "Black"]'
jobj_string = '"Python Json"'
jobj_int = '1234'
jobj_float =  '21.34'
python_dict =  json.loads(jobj_dict)
python_list = json.loads(jobj_list)
python_str =  json.loads(jobj_string)
python_int =   json.loads(jobj_int)
python_float = json.loads(jobj_float)

print("Python dictionary: ", python_dict)
print("Python list: ", python_list)
print("Python string: ", python_str)
print("Python integer: ", python_int)
print("Python float: ", python_float)<jupyter_output>Python dictionary:  {'name': 'David', 'age': 6, 'class': 'I'}
Python list:  ['Red', 'Green', 'Black']
Python string:  Python Json
Python integer:  1234
Python float:  21.34<jupyter_text>Q6. Write a Python program to create a new JSON file from an existing JSON file.<jupyter_code>import json

with open('states.json') as f:
    state_data= json.load(f)


with open('new_states.json', 'w') as f:
    json.dump(state_data, f, indent=2)<jupyter_output><empty_output><jupyter_text>Q7. Write a Python program to check whether an instance is complex or not.<jupyter_code>import json

def encode_complex(object):
    # check using isinstance method
    if isinstance(object, complex):
        return [object.real, object.imag]
    # raised error if object is not complex
    raise TypeError(repr(object) + " is not JSON serialized")

complex_obj = json.dumps(2 + 3j, default=encode_complex)
print(complex_obj)<jupyter_output>[2.0, 3.0]<jupyter_text>Q8. Write a Python program to check whether a JSON string contains complex object or not.<jupyter_code>import json
def is_complex_num(objct):
    if '__complex__' in objct:
        return complex(objct['real'], objct['img'])
    return objct

complex_object =json.loads('{"__complex__": true, "real": 4, "img": 5}', object_hook = is_complex_num)
simple_object =json.loads('{"real": 4, "img": 3}', object_hook = is_complex_num)
print("Complex_object: ",complex_object)
print("Without complex object: ",simple_object)<jupyter_output>Complex_object:  (4+5j)
Without complex object:  {'real': 4, 'img': 3}<jupyter_text>Q9. Write a Python program to access only unique key value of a Python object.<jupyter_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)<jupyter_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}
{'fraction_non_alphanumeric': 0.15942879499217527, 'fraction_numerical': 0.022104851330203443, 'mean_word_length': 5.480354879594423, 'pattern_counts': {'":': 39, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '112214', 'license': 'MIT', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1865, 'n_words': 528, 'path': 'Python Coding/Python Exercise JSON.ipynb', 'repo_name': 'sarojkrjha/PythonWorkSheet', 'size': '5112'}