Q:

Sort JSON keys in and write them into a file using python programming

belongs to collection: Python JSON Exercises

0

Sort JSON keys in and write them into a file

Sort following JSON data alphabetical order of keys

sampleJson = {"id" : 1, "name" : "value2", "age" : 29}

Expected Output:

{
    "age": 29,
    "id": 1,
    "name": "value2"
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import json

sampleJson = {"id" : 1, "name" : "value2", "age" : 29}

print("Started writing JSON data into a file")
with open("sampleJson.json", "w") as write_file:
    json.dump(sampleJson, write_file, indent=4, sort_keys=True)
print("Done writing JSON data into a file")

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Access the value of key2 from the following JSON u...