Write Read from and to a json file
I'm working on a project IN PYTHON where I need to read and write from and to a JSON file.
The JSON file's contents look like this:
{
"buildings": [
{
"name": "Trump Towers",
"nr": "1",
"worth": "399"
},
{
"name": "Penning Towers",
"nr": "2",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Mr Henry",
"nr": "2",
"worth": "299"
}
]
}
Again, I need to be able to read, add and delete the individual buildings' and staff members' data
(THE FOLLOWING IS NOT IN CORRECT SYNTAX, BUT THAT'S WHY I'M ASKING, I NEED HELP WITH THIS)
(syntax not accurate) eg.
>>> read name of building nr 1
Trump Towers
>>> delete 'Trump Towers' from buildings (output to the file)
{
"buildings": [
{
"name": "Penning Towers",
"nr": "2",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Mr Henry",
"nr": "2",
"worth": "299"
}
]
}
>>> set 'Penning Towers' from buildings nr to 1
{
"buildings": [
{
"name": "Penning Towers",
"nr": "1",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Mr Henry",
"nr": "2",
"worth": "299"
}
]
}
>>> add 'Jake' with nr '3' and worth '999' to staff
{
"buildings": [
{
"name": "Penning Towers",
"nr": "1",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Jake",
"nr": "2",
"worth": "299"
},
{
"name": "Mr Henry",
"nr": "3",
"worth": "999"
}
]
}
python json
add a comment |
I'm working on a project IN PYTHON where I need to read and write from and to a JSON file.
The JSON file's contents look like this:
{
"buildings": [
{
"name": "Trump Towers",
"nr": "1",
"worth": "399"
},
{
"name": "Penning Towers",
"nr": "2",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Mr Henry",
"nr": "2",
"worth": "299"
}
]
}
Again, I need to be able to read, add and delete the individual buildings' and staff members' data
(THE FOLLOWING IS NOT IN CORRECT SYNTAX, BUT THAT'S WHY I'M ASKING, I NEED HELP WITH THIS)
(syntax not accurate) eg.
>>> read name of building nr 1
Trump Towers
>>> delete 'Trump Towers' from buildings (output to the file)
{
"buildings": [
{
"name": "Penning Towers",
"nr": "2",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Mr Henry",
"nr": "2",
"worth": "299"
}
]
}
>>> set 'Penning Towers' from buildings nr to 1
{
"buildings": [
{
"name": "Penning Towers",
"nr": "1",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Mr Henry",
"nr": "2",
"worth": "299"
}
]
}
>>> add 'Jake' with nr '3' and worth '999' to staff
{
"buildings": [
{
"name": "Penning Towers",
"nr": "1",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Jake",
"nr": "2",
"worth": "299"
},
{
"name": "Mr Henry",
"nr": "3",
"worth": "999"
}
]
}
python json
JSON is nothing mysterious in Python; it just deserializes to nested lists and dictionaries. How would you go about processing this in pure Python?
– roganjosh
Nov 22 '18 at 20:06
Possible duplicate of How do I write JSON data to a file?
– shkaper
Nov 22 '18 at 21:57
add a comment |
I'm working on a project IN PYTHON where I need to read and write from and to a JSON file.
The JSON file's contents look like this:
{
"buildings": [
{
"name": "Trump Towers",
"nr": "1",
"worth": "399"
},
{
"name": "Penning Towers",
"nr": "2",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Mr Henry",
"nr": "2",
"worth": "299"
}
]
}
Again, I need to be able to read, add and delete the individual buildings' and staff members' data
(THE FOLLOWING IS NOT IN CORRECT SYNTAX, BUT THAT'S WHY I'M ASKING, I NEED HELP WITH THIS)
(syntax not accurate) eg.
>>> read name of building nr 1
Trump Towers
>>> delete 'Trump Towers' from buildings (output to the file)
{
"buildings": [
{
"name": "Penning Towers",
"nr": "2",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Mr Henry",
"nr": "2",
"worth": "299"
}
]
}
>>> set 'Penning Towers' from buildings nr to 1
{
"buildings": [
{
"name": "Penning Towers",
"nr": "1",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Mr Henry",
"nr": "2",
"worth": "299"
}
]
}
>>> add 'Jake' with nr '3' and worth '999' to staff
{
"buildings": [
{
"name": "Penning Towers",
"nr": "1",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Jake",
"nr": "2",
"worth": "299"
},
{
"name": "Mr Henry",
"nr": "3",
"worth": "999"
}
]
}
python json
I'm working on a project IN PYTHON where I need to read and write from and to a JSON file.
The JSON file's contents look like this:
{
"buildings": [
{
"name": "Trump Towers",
"nr": "1",
"worth": "399"
},
{
"name": "Penning Towers",
"nr": "2",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Mr Henry",
"nr": "2",
"worth": "299"
}
]
}
Again, I need to be able to read, add and delete the individual buildings' and staff members' data
(THE FOLLOWING IS NOT IN CORRECT SYNTAX, BUT THAT'S WHY I'M ASKING, I NEED HELP WITH THIS)
(syntax not accurate) eg.
>>> read name of building nr 1
Trump Towers
>>> delete 'Trump Towers' from buildings (output to the file)
{
"buildings": [
{
"name": "Penning Towers",
"nr": "2",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Mr Henry",
"nr": "2",
"worth": "299"
}
]
}
>>> set 'Penning Towers' from buildings nr to 1
{
"buildings": [
{
"name": "Penning Towers",
"nr": "1",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Mr Henry",
"nr": "2",
"worth": "299"
}
]
}
>>> add 'Jake' with nr '3' and worth '999' to staff
{
"buildings": [
{
"name": "Penning Towers",
"nr": "1",
"worth": "299"
}
],
"staff": [
{
"name": "D Trump",
"nr": "1",
"worth": "399"
},
{
"name": "Jake",
"nr": "2",
"worth": "299"
},
{
"name": "Mr Henry",
"nr": "3",
"worth": "999"
}
]
}
python json
python json
edited Nov 22 '18 at 20:12
petezurich
3,73581936
3,73581936
asked Nov 22 '18 at 20:02
GNYGNY
165
165
JSON is nothing mysterious in Python; it just deserializes to nested lists and dictionaries. How would you go about processing this in pure Python?
– roganjosh
Nov 22 '18 at 20:06
Possible duplicate of How do I write JSON data to a file?
– shkaper
Nov 22 '18 at 21:57
add a comment |
JSON is nothing mysterious in Python; it just deserializes to nested lists and dictionaries. How would you go about processing this in pure Python?
– roganjosh
Nov 22 '18 at 20:06
Possible duplicate of How do I write JSON data to a file?
– shkaper
Nov 22 '18 at 21:57
JSON is nothing mysterious in Python; it just deserializes to nested lists and dictionaries. How would you go about processing this in pure Python?
– roganjosh
Nov 22 '18 at 20:06
JSON is nothing mysterious in Python; it just deserializes to nested lists and dictionaries. How would you go about processing this in pure Python?
– roganjosh
Nov 22 '18 at 20:06
Possible duplicate of How do I write JSON data to a file?
– shkaper
Nov 22 '18 at 21:57
Possible duplicate of How do I write JSON data to a file?
– shkaper
Nov 22 '18 at 21:57
add a comment |
2 Answers
2
active
oldest
votes
You can use the json module to load the file with json.load() in to a Python Dictionary:
import json
f = open('file.json', 'r')
d = json.load(f)
Once it's a python dict, you can modify it as you like.
You can then write the the json to a file with json.dump(d)
add a comment |
You can use json library to load json from your file as python dict and then modify that json and save it back as file.
import json
# Open json file and load its content as python dict
file = open('data.json', 'r')
my_json = json.loads(file.read())
file.close()
# Do stuff with that json
del my_json['buildings'][0]
my_json['buildings'][0]['Penning Towers'] = 1
my_json['staff'].append({'name': 'Jake', 'nr': '3', 'worth': '299'})
# Override json file with modified json
file = open('data.json', 'w')
file.write(json.dumps(my_json, indent=4))
file.close()
data.json after running our code:
{
"staff": [
{
"nr": "1",
"worth": "399",
"name": "D Trump"
},
{
"nr": "2",
"worth": "299",
"name": "Mr Henry"
},
{
"nr": "3",
"worth": "299",
"name": "Jake"
}
],
"buildings": [
{
"nr": "1",
"worth": "299",
"name": "Penning Towers"
}
]
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437425%2fwrite-read-from-and-to-a-json-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the json module to load the file with json.load() in to a Python Dictionary:
import json
f = open('file.json', 'r')
d = json.load(f)
Once it's a python dict, you can modify it as you like.
You can then write the the json to a file with json.dump(d)
add a comment |
You can use the json module to load the file with json.load() in to a Python Dictionary:
import json
f = open('file.json', 'r')
d = json.load(f)
Once it's a python dict, you can modify it as you like.
You can then write the the json to a file with json.dump(d)
add a comment |
You can use the json module to load the file with json.load() in to a Python Dictionary:
import json
f = open('file.json', 'r')
d = json.load(f)
Once it's a python dict, you can modify it as you like.
You can then write the the json to a file with json.dump(d)
You can use the json module to load the file with json.load() in to a Python Dictionary:
import json
f = open('file.json', 'r')
d = json.load(f)
Once it's a python dict, you can modify it as you like.
You can then write the the json to a file with json.dump(d)
edited Nov 22 '18 at 20:31
answered Nov 22 '18 at 20:07
AdamAdam
384113
384113
add a comment |
add a comment |
You can use json library to load json from your file as python dict and then modify that json and save it back as file.
import json
# Open json file and load its content as python dict
file = open('data.json', 'r')
my_json = json.loads(file.read())
file.close()
# Do stuff with that json
del my_json['buildings'][0]
my_json['buildings'][0]['Penning Towers'] = 1
my_json['staff'].append({'name': 'Jake', 'nr': '3', 'worth': '299'})
# Override json file with modified json
file = open('data.json', 'w')
file.write(json.dumps(my_json, indent=4))
file.close()
data.json after running our code:
{
"staff": [
{
"nr": "1",
"worth": "399",
"name": "D Trump"
},
{
"nr": "2",
"worth": "299",
"name": "Mr Henry"
},
{
"nr": "3",
"worth": "299",
"name": "Jake"
}
],
"buildings": [
{
"nr": "1",
"worth": "299",
"name": "Penning Towers"
}
]
}
add a comment |
You can use json library to load json from your file as python dict and then modify that json and save it back as file.
import json
# Open json file and load its content as python dict
file = open('data.json', 'r')
my_json = json.loads(file.read())
file.close()
# Do stuff with that json
del my_json['buildings'][0]
my_json['buildings'][0]['Penning Towers'] = 1
my_json['staff'].append({'name': 'Jake', 'nr': '3', 'worth': '299'})
# Override json file with modified json
file = open('data.json', 'w')
file.write(json.dumps(my_json, indent=4))
file.close()
data.json after running our code:
{
"staff": [
{
"nr": "1",
"worth": "399",
"name": "D Trump"
},
{
"nr": "2",
"worth": "299",
"name": "Mr Henry"
},
{
"nr": "3",
"worth": "299",
"name": "Jake"
}
],
"buildings": [
{
"nr": "1",
"worth": "299",
"name": "Penning Towers"
}
]
}
add a comment |
You can use json library to load json from your file as python dict and then modify that json and save it back as file.
import json
# Open json file and load its content as python dict
file = open('data.json', 'r')
my_json = json.loads(file.read())
file.close()
# Do stuff with that json
del my_json['buildings'][0]
my_json['buildings'][0]['Penning Towers'] = 1
my_json['staff'].append({'name': 'Jake', 'nr': '3', 'worth': '299'})
# Override json file with modified json
file = open('data.json', 'w')
file.write(json.dumps(my_json, indent=4))
file.close()
data.json after running our code:
{
"staff": [
{
"nr": "1",
"worth": "399",
"name": "D Trump"
},
{
"nr": "2",
"worth": "299",
"name": "Mr Henry"
},
{
"nr": "3",
"worth": "299",
"name": "Jake"
}
],
"buildings": [
{
"nr": "1",
"worth": "299",
"name": "Penning Towers"
}
]
}
You can use json library to load json from your file as python dict and then modify that json and save it back as file.
import json
# Open json file and load its content as python dict
file = open('data.json', 'r')
my_json = json.loads(file.read())
file.close()
# Do stuff with that json
del my_json['buildings'][0]
my_json['buildings'][0]['Penning Towers'] = 1
my_json['staff'].append({'name': 'Jake', 'nr': '3', 'worth': '299'})
# Override json file with modified json
file = open('data.json', 'w')
file.write(json.dumps(my_json, indent=4))
file.close()
data.json after running our code:
{
"staff": [
{
"nr": "1",
"worth": "399",
"name": "D Trump"
},
{
"nr": "2",
"worth": "299",
"name": "Mr Henry"
},
{
"nr": "3",
"worth": "299",
"name": "Jake"
}
],
"buildings": [
{
"nr": "1",
"worth": "299",
"name": "Penning Towers"
}
]
}
edited Nov 22 '18 at 20:58
answered Nov 22 '18 at 20:11
Filip MłynarskiFilip Młynarski
1,7811414
1,7811414
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437425%2fwrite-read-from-and-to-a-json-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
JSON is nothing mysterious in Python; it just deserializes to nested lists and dictionaries. How would you go about processing this in pure Python?
– roganjosh
Nov 22 '18 at 20:06
Possible duplicate of How do I write JSON data to a file?
– shkaper
Nov 22 '18 at 21:57