Aakash Bansal Aakash Bansal - 1 month ago 26
JSON Question

The JSON sent by firebase is invalid

{ name: 'anonymous', text: 'Hello' }
{ name: 'anonymous', text: 'How are you' }
{ name: 'anonymous', text: 'I am fine' }


This code is returned by firebase, and I'm getting the error "No JSON object could be decoded". I think this has to do something with the validity of json format.


I'm getting this json data using the firebase node.js SDK. Then I'm passig it to python using pyshell. When I use the
json.loads
in python. It says :



C:\Python27>node firebase2.js
{ name: 'anonymous', text: 'Hello' }
{ name: 'anonymous', text: 'How are you' }
{ name: 'anonymous', text: 'I am fine' }
C:\Python27\firebase2.js:40
if (err) throw err;
^

Error: ValueError: No JSON object could be decoded
at PythonShell.parseError (C:\Python27\node_modules\python-shell\index.js:183:17)
at terminateIfNeeded (C:\Python27\node_modules\python-shell\index.js:98:28)
at ChildProcess.<anonymous> (C:\Python27\node_modules\python-shell\index.js:88:9)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)
----- Python Traceback -----
File "my_script.py", line 3, in <module>
myjson = json.loads(myinput)
File "C:\Python27\lib\json\__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")

rsp rsp
Answer

This is not a valid JSON and I think that your firebase2.js is at fault here.

Instead of this:

{ name: 'anonymous', text: 'Hello' }
{ name: 'anonymous', text: 'How are you' }
{ name: 'anonymous', text: 'I am fine' }

It should output this:

[
  { "name": "anonymous", "text": "Hello" },
  { "name": "anonymous", "text": "How are you" },
  { "name": "anonymous", "text": "I am fine" }
]

All strings (including object keys) have to be quoted with double quotes. Arrays have to be included in square brackets and array elements need to be delimited with commas.

Check out your firebase2.js program and see how it generates its output. If it uses anything else than a single:

console.log(JSON.stringify(SOME_VARIABLE));

Then here's your problem.

In any case, I am more than sure that Firebase is not returning {a:'b'}{c:'d'} instead of [{"a":"b"},{"c":"d"}] - this is a typical error of beginners who don't know the JSON format, something hard to believe in the case of one of the biggest API providers in the world.

If you want to know what is the real response then use curl:

curl -v https://example.com/some/endpoint -H 'auth header' ...

and if you see invalid JSON there, then it's time to contact Firebase support.

The JSON format is explained on http://json.org/ - this is the simplest data format in existence.

Comments