Unexpected token '&' while reading json object as string in javascript - javascript
I have a model which will read a json object as a string.
string zipCodes = "[{'zip': '06854','market_area': 'R01','state': 'CT'},
{'zip': '06842','market_area': 'R01','state': 'CT'},
{'zip': '02765','market_area': 'R02','state': 'TN'}]"
I am trying to read this string as a constant in javascript.
const array = #Model.zipCodes;
But the value is returning as
const array = [{'zip': '06854','market_area':
'R01','state': 'CT'},
{'zip': '06842','market_area':
'R01','state': 'CT'},
{'zip': '02765','market_area':
'R02','state': 'TN'}];
and throwing an unexpected token '&' error.
How can we handle this error.
Thank you.
Related
TweetNaCl TypeError: unexpected type, use Uint8Array
I have a problem when trying to use sign function from TweetNaCl. It always throws error unexpected type, use Uint8Array. const nacl = require('tweetnacl') nacl.util = require('tweetnacl-util') const b = nacl.sign.keyPair() // console.log(b.secretKey) the result is already in Uint8Array form const c = b.secretKey const sign= nacl.sign("message", c) //error unexpected type, use Uint8Array nacl.sign.keyPair() wil return an object containing secretkey and publickey .. but when trying to sign nacl.sign("message", privatekey) error is trown Anyone knows how to overcome this? thank you :)
Use Hidden form field to store JSON and then parse it. JSON parse fails
Please excuse if this is duplicate. I have a hidden form field and I store a base64 encoded string in which represents a serialised object in JSON format. When I try to deserialise the object, I get a parse error. The form value after it has been base64 decoded results in a "string", and trying to use JSON.parse(decoded_field_value) causes a parsing error, and a OBJECT cannot be created. Here are some snippets of the relevant code: Controller C# private string ToBase64(string str) { byte[] bytes = System.Text.Encoding.Unicode.GetBytes(str); return Convert.ToBase64String(bytes); } // the model holds the hidden form field value model.Qt = ToBase64(JsonConvert.SerializeObject(qTypes)); Cshtml: #Html.HiddenFor(model => model.Qt) RawHtml: <input id="b64Field" name="Qt" type="hidden" value="WwB7ACIASQBkACIAOgAxACwAIgBO......=="/> JavaScript: var field = $('input:hidden[name="Qt"]').val(); var ds = atob(field); var p = JSON.parse(ds); Values: field = "WwB7ACIASQBkACIAOgAxACwAIgBOAG...." ds = "[{"Id":1,"X":"ValX"},{"Id":2,"Y":"ValY"}]" Error Message: VM1346:1 Uncaught SyntaxError: Unexpected token in JSON at position 1 at JSON.parse (<anonymous>) I can see the problem is that the ds is not valid JSON, as it should be ds = [{"Id":1,"X":"ValX"},{"Id":2,"Y":"ValY"}] How do I fix this error using Javascript or C# in the backend.
JSON Parse not working, parsing two dimensional array
I am trying to parse this LOCAL XMLhttprequest, I am getting back the right response text and displaying it in safari. When I make an object to JSON.parse() the responsetext,I am getting errors like "unidentified token '"' " or "expected '}' " no matter how I change the .txt file, it will not parse into an object for me I have tried to change the .txt to the right JSON format with no luck {playerGrid: [["3","2","2","2","2","2","2","3","3","3"], ["3","2","2","2","2","2","2","2","2","2"], ["3","2","2","2","2","2","2","2","2","3"],["3","2","2","2","2","2","2","2","2","3"], ["4","2","2","2","2","3","2","2","2","3"], ["2","2","2","2","7","3","2","2","2","3"], ["2","2","2","2","7","2","2","2","2","2"], ["2","2","2","3","3","3","2","2","2","2"], ["2","2","2,"2","2","2","2","2","2","2"], ["2","2","2","2","2","2","2","2","2","2"]], computerGrid: [["2","2","2","7","4","9","9","2","2","2"], ["2","9","2","2","2","2","2","2","2","2"], ["2","9","2","2","2","2","2","2","2","2"], ["2","9","2","2","9","9","2","2,"2","2"], ["2","2","2","2","2","2","2","2","2","2"], ["9","9","9","9","9","2","2","2","2","2"], ["2","2","2","2","7","2","2","2","9","2"], ["2","2","2","2","2","2","2","2","9","2"], ["2","2","2","2","2","2","2","2","9","2"], ["2","2","2","2","2","2","2","2","9","2"]]}; here is my JOSN .txt function fileRequest() { var localRequest = new XMLHttpRequest(); localRequest.open("GET", "sampleJSON.txt", false); localRequest.send(null); document.getElementById("jsonDiv").innerHTML = localRequest.responseText; var jsonObject = JSON.parse(localRequest.response); document.getElementById("jsonParsed").innerHTML = jsonObject.computerGrid; } here is my simple function, I first display the response, and then it errors when I am trying to parse the data. Thanks I expect an object that I can .computerGrid or .playerGrid.
You have some errors in your json, just search by "2,, there are 2 occurences, it should be "2",. You are missing a closing ". Also, in order to the JSON to be valid, it should look like this: {"playerGrid":[["3","2","2","2","2","2","2","3","3","3"],["3","2","2","2","2","2","2","2","2","2"],["3","2","2","2","2","2","2","2","2","3"],["3","2","2","2","2","2","2","2","2","3"],["4","2","2","2","2","3","2","2","2","3"],["2","2","2","2","7","3","2","2","2","3"],["2","2","2","2","7","2","2","2","2","2"],["2","2","2","3","3","3","2","2","2","2"],["2","2","2","2","2","2","2","2","2","2"],["2","2","2","2","2","2","2","2","2","2"]],"computerGrid":[["2","2","2","7","4","9","9","2","2","2"],["2","9","2","2","2","2","2","2","2","2"],["2","9","2","2","2","2","2","2","2","2"],["2","9","2","2","9","9","2","2","2","2"],["2","2","2","2","2","2","2","2","2","2"],["9","9","9","9","9","2","2","2","2","2"],["2","2","2","2","7","2","2","2","9","2"],["2","2","2","2","2","2","2","2","9","2"],["2","2","2","2","2","2","2","2","9","2"],["2","2","2","2","2","2","2","2","9","2"]]} Keys, playerGrid and computerGrid must be between ".
Use JSON.parse(localRequest.responseText) Your JSON is incorrect, you are missing quotes around some numbers like "2, Object Keys "playerGrid" and "computerGrid" must be quoted too let d = `{ "playerGrid": [ ["3","2","2","2","2","2","2","3","3","3"], ["3","2","2","2","2","2","2","2","2","2"], ["3","2","2","2","2","2","2","2","2","3"], ["3","2","2","2","2","2","2","2","2","3"], ["4","2","2","2","2","3","2","2","2","3"], ["2","2","2","2","7","3","2","2","2","3"], ["2","2","2","2","7","2","2","2","2","2"], ["2","2","2","3","3","3","2","2","2","2"], ["2","2","2","2","2","2","2","2","2","2"], ["2","2","2","2","2","2","2","2","2","2"]], "computerGrid": [ ["2","2","2","7","4","9","9","2","2","2"], ["2","9","2","2","2","2","2","2","2","2"], ["2","9","2","2","2","2","2","2","2","2"], ["2","9","2","2","9","9","2","2","2","2"], ["2","2","2","2","2","2","2","2","2","2"], ["9","9","9","9","9","2","2","2","2","2"], ["2","2","2","2","7","2","2","2","9","2"], ["2","2","2","2","2","2","2","2","9","2"], ["2","2","2","2","2","2","2","2","9","2"], ["2","2","2","2","2","2","2","2","9","2"]] }`; console.log(JSON.parse(d)) PS: you can always validate your json with codebeautify.org/jsonvalidator
SyntaxError: Unexpected token r in JSON at position 38
I was unexpected token r in JSON at position 38 for this code. var object = JSON.parse('{"isFaceboook" : true,"redirectUrl" : redUrl,"facebookId" : id}'); redUrl and id are initialized and they are strings...
Your variables will need to be stringified as well before it can be parsed. Something like: '{"isFacebook":true,"redirectUrl":"redUrl","facebookId":4}'
There's no point to making a JSON string and parsing it: var object = { isFacebook: true, redirectUrl: redUrl, facebookId: id }; will work.
Using JS to connect to an API and then output the JSON response
I'm trying to do a UK Postcode validation using JS by connecting to http://postcodes.io/ post code validation service. When I post to api.postcodes.io/postcodes/POSTCODEVARIABLE/validate I should recieve a 200 response and result of either "True" or "False" depending on whether the postcode is valid or not. However, when I try to receive the data (in JSON format) I get an error in the console: "Uncaught SyntaxError: Unexpected token o in JSON at position 1" Here is my code, if anyone could point out where I am going wrong, that would be great. var cpostcode = 'RG1 8BT'; var cpostcode = cpostcode.replace(/\s+/g, ''); var myNewURL = 'https://api.postcodes.io/postcodes/' + cpostcode + '/validate'; var pcapi = new XMLHttpRequest(); pcapi.onreadystatechange = function() { if (pcapi.readyState == XMLHttpRequest.DONE) { alert(pcapi.responseText); } } pcapi.open("GET", myNewURL, true); pcapi.send(null); var xyz = JSON.parse(pcapi); console.log(xyz);
Like at #Youssef said you want to log the .responseText. You get that script error because pcapi is a XMLHttpRequest object and not valid json which JSON.parse() attempts to parse. If you want to learn more about what you can do with the pcapi object just console.log(pcapi) and you can see its {keys:values} or just read up on it. JSON.parse() converts any JSON String passed into the function, to a JSON Object. There is really no such thing as a "JSON Object". The JSON spec is a syntax for encoding data as a string. ... JSON is a subset of the object literal notation of JavaScript. In other words, valid JSON is also valid JavaScript object literal notation but not necessarily the other way around. - stackoverflow var cpostcode = 'RG1 8BT'; var cpostcode = cpostcode.replace(/\s+/g, ''); var pcapi = new XMLHttpRequest(); var myNewURL = 'https://api.postcodes.io/postcodes/' + cpostcode + '/validate'; pcapi.open("GET", myNewURL, false); pcapi.send(); var xyz = JSON.parse(pcapi.responseText); console.log(xyz);