I want to add new element to json object to specific index/position.
if I use data["country"] = "value"
, it is adding to the end of the json object.
import json
data = json.loads('''{"user_name": "xcv","password": "dsjvwebv","age":27,"address":{"country_name": "value",
"state_name":"tamil nadu", "district" :"Tirunelveli" },"work_history": [ {"name": "CSC",
"location": "chennai"}, {"name": "Saturam", "location": "bangalore"}, {"name": "crayon",
"location": "chennai"} ],"marital_status" :"married","disability":"No"}''')
del data["password"]
country = data["address"]["country_name"]
data.pop("disability")
data.pop("address")
data["country"] = country
i=0
for j in data["work_history"]:
if j["location"] != "chennai":
data["work_history"].pop(i)
i = i+1
print(data)
i want the country value to be at position as in the output below.
{'user_name': 'xcv', 'age': 27, 'country': 'value', 'work_history': [{'name': 'CSC', 'location': 'chennai'}, {'name': 'crayon', 'location': 'chennai'}], 'marital_status': 'married}