2

I have a string and I want to decode into json. The string was originally base64. Whne I try and decode into jason I get the below error.

  var query_string = new Buffer(bid, 'base64').toString('ascii');
  console.log(query_string);
  var q = JSON.parse(query_string);


{'avid': '[email protected]', 'crid': '20767073515', 'mabid': {'node': None, 'hod': '13', 'cid': '36', 'industry': None, 'ex': '1', 'vid1': '29', 'dow': '3'}, 'prid': {'hod': '13', 'woy': '18', 'cid': '36', 'dow': '3', 'ssp': 'adx', 'st': None, 'bt': 'firefox', 'cty': 'tokyo', 'ex': '1', 'vid2': '222', 'dt': '1', 'os': 'mac', 'vid1': '29'}, 'agid': '4547917795', 'cookieid': 'retageting:cookie', 'did': 'yahoo.com', 'validation': True}

SyntaxError: Unexpected token ' at Object.parse (native) at /home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/app.js:115:16 at callbacks (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:272:11) at param (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:246:11) at pass (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:253:5) at Router._dispatch (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:280:4) at Object.handle (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/lib/router/index.js:45:10) at next (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/node_modules/connect/lib/http.js:204:15) at Object.methodOverride [as handle] (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5) at next (/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/express/node_modules/connect/lib/http.js:204:15)

2 Answers 2

5

JSON format requires double quotes, not single quotes.

Also:

  • None should be: null
  • True should be lowercase: true

query_string should look like the following:

{"avid": "[email protected]", "crid": "20767073515", "mabid": {"node": null, "hod": "13", "cid": "36", "industry": null, "ex": "1", "vid1": "29", "dow": "3"}, "prid": {"hod": "13", "woy": "18", "cid": "36", "dow": "3", "ssp": "adx", "st": null, "bt": "firefox", "cty": "tokyo", "ex": "1", "vid2": "222", "dt": "1", "os": "mac", "vid1": "29"}, "agid": "4547917795", "cookieid": "retageting:cookie", "did": "yahoo.com", "validation": true}

I guess that's a Python dictionary, you should be using a library to serialize a python dictionary correctly to JSON, or if you're using Python 2.6+, simply do:

import json
json_string = json.dumps({'test': 'test'})

Docs: http://docs.python.org/library/json.html

Sign up to request clarification or add additional context in comments.

2 Comments

Hmmmm....well...the string started out as a a python dictionary, converted to jason then to base64. Python can decode but guess Node cant
Ahhhhhhh......dumb me....I was originally using tornado...it was all in the same family. I used import ast to decode string to a python dic.
1

JSON needs double quotes around keys and (string) values, not single quotes.

Also:

  1. None is not a legal value - the JSON way to encode an empty key is "mykey": null
  2. True and False must be in lower case

The formal grammar for JSON is on the front page at http://www.json.org/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.