python tutorial - JSON.DUMP(S) & JSON.LOAD(S) - learn python - python programming




json.dumps() & json.loads()

  • In this tutorial, we'll convert Python dictionary to JSON and write it to a text file. Then, we'll read in back from the file and play with it.

Initially we'll construct Python dictionary like this:

# Four Fundamental Forces with JSON
d = {}

d ["gravity"] = {
"mediator":"gravitons",
"relative strength" : "1",
"range" : "infinity"
}
d ["weak"] = {
"mediator":"W/Z bosons",
"relative strength" : "10^25",
"range" : "10^-18"
}
d ["electromagnetic"] = {
"mediator":"photons",
"relative strength" : "10^36",
"range" : "infinity"
}
d ["strong"] = {
"mediator":"gluons",
"relative strength" : "10^38",
"range" : "10^-15"
}

print(d)
click below button to copy the code. By Python tutorial team

The output looks like this:

{'electromagnetic': {'relative strength': '10^36', 'range': 'infinity', 'mediator': 'photons'}, 'strong': {'relative strength': '10^38', 'range': '10^-15', 'mediator': 'gluons'}, 'weak': {'relative strength': '10^25', 'range': '10^-18', 'mediator': 'W/Z bosons'}, 'gravity': {'relative strength': '1', 'range': 'infinity', 'mediator': 'gravitons'}}

Now, we want to convert the dictionary to a string using json.dumps:

import json
data = json.dumps(d)
print(type(data))
print(data)
click below button to copy the code. By Python tutorial team

Output:

<type 'str'>
{"electromagnetic": {"relative strength": "10^36", "range": "infinity", "mediator": "photons"}, "strong": {"relative strength": "10^38", "range": "10^-15", "mediator": "gluons"}, "weak": {"relative strength": "10^25", "range": "10^-18", "mediator": "W/Z bosons"}, "gravity": {"relative strength": "1", "range": "infinity", "mediator": "gravitons"}}
  • Note that the "json.dumps()" returns a string as indicated by the "s" at the end of "dumps". This process is called encoding.

Let's write it to a file:

import json
data = json.dumps(d)
with open("4forces.json","w") as f:
  f.write(data)
click below button to copy the code. By Python tutorial team

Now that the file is written. Let's reads it back and decoding the JSON-encoded string back into a Python dictionary data structure:

# reads it back
with open("4forces.json","r") as f:
  data = f.read()

# decoding the JSON to dictionay
d = json.loads(data)
click below button to copy the code. By Python tutorial team
  • Let's play with the dictionary a little bit.

What's the relative strength of electromagnetic compared to gravity?

print(d["electromagnetic"]["relative strength"])
10^36
click below button to copy the code. By Python tutorial team

Who's the mediator for "strong" force?

print(d["strong"]["mediator"])
gluons
click below button to copy the code. By Python tutorial team
  • Ok, here is the full code:
# Four Fundamental Forces with JSON
d = {}

d ["gravity"] = {
"mediator":"gravitons",
"relative strength" : "1",
"range" : "infinity"
}
d ["weak"] = {
"mediator":"W/Z bosons",
"relative strength" : "10^25",
"range" : "10^-18"
}
d ["electromagnetic"] = {
"mediator":"photons",
"relative strength" : "10^36",
"range" : "infinity"
}
d ["strong"] = {
"mediator":"gluons",
"relative strength" : "10^38",
"range" : "10^-15"
}

import json

# encoding to JSON
data = json.dumps(d)

# write to a file
with open("4forces.json","w") as f:
  f.write(data)

# reads it back
with open("4forces.json","r") as f:
  data = f.read()

# decoding the JSON to dictionay
d = json.loads(data)

print(d)
click below button to copy the code. By Python tutorial team

If we prefer working with files instead of strings, we may want to use json.dump() / json.load() to encode / decode JSON data:

# write to a file
with open("4forces.json","w") as f:
  json.dump(d, f)

# reads it back
with open("4forces.json","r") as f:
  d = json.load(f)
click below button to copy the code. By Python tutorial team

Related Searches to JSON.DUMP(S) & JSON.LOAD(S)

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×