简体   繁体   中英

Node.js cant parse a JSON string from base64 decode

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': 'info@tssf.co.jp', '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)

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": "info@tssf.co.jp", "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

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM