PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 23, 2022

[FIXED] How can i add some element into json file using python?

 July 23, 2022     json, python-3.x     No comments   

Issue

this is json {"name": ["Juan", "Alex"]}. how can i add something to "name", using python? thanks in advance for your reply. This is my code, he is don't work :(

import json

enteredString = str(input())
json_file = 'list_of_workers.json'
data = json.load(open(json_file, "rb"))
data['name'].append(enteredString)
json.dump(data, open(json_file, "wb"))

Solution

import json

enteredString = str(input())
json_file = 'list_of_workers.json' 
data = json.load(open(json_file, "rb"))
data['name'].append(enteredString)
newdata = json.dumps(data, indent=4, sort_keys=True)  
with open(json_file, 'w') as f:   
    f.write(newdata)


Answered By - Ari Abimanyu
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing