user11502311
user11502311

Reputation:

ValueError. Cannot json.load file that I just json.dumped in Python2

With the following code, where I dump a file into a .json file and then read it again to create a response, I get a ValueError.

with open(key, 'w+') as outfile:
    if content_as_json:
        json.dump(file, outfile)
        response = self._createStoreResponse(status_code=200, content =  json.load(outfile))
    else:
        outfile.write(file)
        response = self._createStoreResponse(status_code=200, content = outfile.readlines())

The traceback is at the bottom. I can confirm that the file in question is actually created. If I do a cat, it looks like a valid json file. See:

/nubera$ cat test_svx-environment.json                                                                                                                              │
{"type": "test_sv", "name": "test_svx-environment", "description": "Testing ground environment of test_sv"}

event: None                                                                                                                                                                                        │
__format_message event: None                                                                                                                                                                       │
event: unknown, log_type: info, message: No document found yet. Creating document with data: {"type": "test_sv", "name": "test_svx-environment", "description": "Testing ground environment of test│
_sv"}                                                                                                                                                                                              │
event: None                                                                                                                                                                                        │
__format_message event: None                                                                                                                                                                       │
event: unknown, log_type: info, message: LocalFileSystemStore: Doing put on:/database/nubera . For File: {"type": "test_sv", "name": "test_svx-environment", "description": "Testing ground environ│
ment of test_sv"}                                                                                                                                                                                  │
[2019-05-22 10:38:56,452] ERROR in app: Exception on /types/test_sv [POST]                                                                                                                         │
Traceback (most recent call last):                                                                                                                                                                 │
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request                                                                                                  │
    rv = self.dispatch_request()                                                                                                                                                                   │
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request                                                                                                       │
    return self.view_functions[rule.endpoint](**req.view_args)                                                                                                                                     │
  File "/usr/local/lib/python2.7/site-packages/flask_restplus/api.py", line 325, in wrapper                                                                                                        │
    resp = resource(*args, **kwargs)                                                                                                                                                               │
  File "/usr/local/lib/python2.7/site-packages/flask/views.py", line 88, in view                                                                                                                   │
    return self.dispatch_request(*args, **kwargs)                                                                                                                                                  │
  File "/usr/local/lib/python2.7/site-packages/flask_restplus/resource.py", line 44, in dispatch_request                                                                                           │
    resp = meth(*args, **kwargs)                                                                                                                                                                   │
  File "./api/types_api.py", line 61, in post                                                                                                                                                      │
  File "./manager/document_manager.py", line 37, in write_type_document                                                                                                                            │
  File "./configuration_store/local_file_system_store.py", line 68, in put                                                                                                                         │
  File "/usr/local/lib/python2.7/json/__init__.py", line 291, in load                                                                                                                              │
    **kw)                                                                                                                                                                                          ├─────────────────────────────────────────
  File "/usr/local/lib/python2.7/json/__init__.py", line 339, in loads                                                                                                                             │ sven  ~  .ssh  
    return _default_decoder.decode(s)                                                                                                                                                              │
  File "/usr/local/lib/python2.7/json/decoder.py", line 364, in decode                                                                                                                             │
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())                                                                                                                                              │
  File "/usr/local/lib/python2.7/json/decoder.py", line 382, in raw_decode                                                                                                                         │
    raise ValueError("No JSON object could be decoded")                                                                                                                                            │
ValueError: No JSON object could be decoded  

Upvotes: 0

Views: 71

Answers (1)

Will
Will

Reputation: 1551

Before you can load the JSON from file, you need to have properly written it. Meaning either:

  • your with context needs to complete so that outfile.close() is called.
  • you've flushed the data before reading it

You also need to open the file handle for reading before using json.load.

with open(key, 'w+') as outfile:
    json.dump(file, outfile)

with open(key, 'r') as infile:
    reread_data = json.load(infile)  

Upvotes: 1

Related Questions