1

I'm using Python to create a data.json file and write a json object to it.

with open('data.json', 'w', encoding='utf-8') as f:
        util.json.dump(jsonData, f, ensure_ascii=False, indent=4)

where jsonData = {'Book': {'author': 'John Black', 'description': 'When....

When I locate data.json file on my computer and open it to modify the content, instead of {'Book': {'author':... I see null printed in the file. I don't understand why it is happening, jsonData is not null, I printed it out before manipulating to double-check. Thank you for your help in advance! =)

3
  • what i s 'jsonData'? Can you paste the full code what you are doing ? Commented Sep 30, 2019 at 6:41
  • Can't figure out why use util.json.dump. Just using json.dump() works fine here Commented Sep 30, 2019 at 6:43
  • Hello and welcome to SO. You may want to read How to Ask and minimal reproducible example and edit your question accordingly. Commented Sep 30, 2019 at 7:12

2 Answers 2

1

I am not sure what purpose util is fulfilling here but using json library seems to be giving right results.

import json

jsonData = {'Book': {'author': 'John Black', 'description': 'When....'}}

with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(jsonData, f, ensure_ascii=False, indent=4)
Sign up to request clarification or add additional context in comments.

Comments

0
import json
jsonData = {
    "Book": {
        "author": "ohn Black",
        "description": "afasffsaf afafasfsa"
    }
}

with open('data.json', 'w', encoding='utf-8') as f:
   f.write(json.dumps(jsonData))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.