access formatted value with special character in javascript array - javascript

I am getting an object as response for an api call
The object looks like below
Now when I try response._modifiedby_value I am getting the right response, but when I try response.createdby_value#OData.Community.Display.V1.FormattedValue I am getting the below error
Uncaught SyntaxError {message: "Unexpected token ILLEGAL"}

that's because '#' is a special character, therefore you can't do:
response.createdby_value#OData = 2;
but instead
response["createdby_value#OData"] = 2;

Related

JSON Parsing in AngularJS Service

I have Angular service and in that service I have a code:
this.testMethod = function( ) {
this.jsonTest = "[{'id':'123','title':'XYZ','id2':'456','status':2}]";
this.parseTest(this.jsonTest);
};
this.parseTest = function(jsonTest) {
var jsonTestObj = JSON.parse(jsonTest); // I get error hear
}
Test method is getting called on ng-click event.
Error that I am getting while testing in latest chrome browser:
SyntaxError: Unexpected token ' at Object.parse (native)
......
I tried multiple things to fix it but nothing seems to work.
Every time I get undefined value error.
Basically I want to parse JSON object and get the values.
What am I doing wrong?
Use this
this.jsonTest = '[{"id":"123","title":"XYZ","id2":"456","status": "2"}]';
Or this
this.jsonTest = "[{\"id\":\"123\",\"title\":\"XYZ\",\"id2\":\"456\",\"status\": \"2\"}]";
You either need to use ' outside or " outside and escape the quotes inside.
Both the key and value need to be in double quotes.
You need to use double quotes in your string, other than that your code should work.
To quote from specification
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
Your JSON is not valid. Always check your JSON integrity in those cases, i personaly use
http://jsonlint.com/

SyntaxError: Illegal return statement - searching for string in url

I'm just in the console in Chrome for now. I created this variable:
url = window.location.href
which gives:
url
"http://www.example.com/trips/dest/australia-and-south-pacific/cntry/fiji/"
Now I tried this:
if (url.indexOf('australia-and-south-pacific') > 0) {return 'foo';}
Which returned:
SyntaxError: Illegal return statement
I expected the console to return 'foo'.
Why is this happening? I noticed that after receiving this error, give it afew seconds and a new error appears:
Uncaught TypeError: Cannot read property 'hide' of undefined
If that helps to diagnose?
According to the EMCAScript language specification,
An ECMAScript program is considered syntactically incorrect if it
contains a return statement that is not within a FunctionBody.

Getting 'Uncaught SyntaxError: Unexpected token' trying to parse JSON

I'm getting an 'Uncaught SyntaxError' while trying to parse a JSON string, and I can't figure out why.
It looks like message is a string, which seems to be a common problem, and the json appears to be valid. A snippet of my code is given. It fails on the var obj = ... line.
this.send = function (message) {
console.log(message);
console.log(message.toString());
console.log('{"ReadyToGo":1}');
console.log(typeof message);
var obj = $.parseJSON(message);
}
On the console, I get this just before the error.:
{"ReadyToGo":1}
{"ReadyToGo":1}
{"ReadyToGo":1}
string
Any ideas?
EDIT: Added console.log(typeof message), which yields 'string'
OK, I got this sorted. A null character ( '\0' ) was been appended to the string somewhere along the long call chain, and this wasn't visible in the debugger. Thanks to cookie monster for the heads-up on this one.
Everything works as soon as I strip out the null char.

Javascript: String concatenation in object constructor

Been trying to create a JavaScript object member that always contains a common string. Whenever I create a new object, instead of concatenating the string, it overwrites it with the passed value on creation. If it matters (I don't think it does) the string contains numbers. Par example:
function myObj(strToConcat){
this.combinedString = "Hello " + strToConcat, /* have used + and .concat() without success */
}
var newObj = new myObj("1.2.3");
console.log(newObj.combinedString); /* says "1.2.3", the leading "Hello " is absent */
Can't seem to get this to concatenate the strings.
EDIT: I apologize, the error was outside the code that I thought responsible. Disregard please. My apologies.
You have error in your reference
console.log(myObj.combinedString);
should be
console.log(newObj.combinedString);
Running your code gives me SyntaxError: Unexpected token }. Replace the , at the end of the second line with a ; and I get the expected result of "Hello 1.2.3".

Weird JSON parsing behavior in js, "Unexpected token :"

As demonstrated in this jsfiddle, if you have a JS file and you create a JSON object without using it, it behaves differently depending on whether the keys(members) are wrapped in quotes or not.
valid code:{ a: 1};
invalid code: { "a": 1 };
What you will get is an error message (in Chrome, different for FF/IE, but still fails on syntax)
Uncaught SyntaxError: Unexpected token :
but if you use the object in some way, for example: alert({ "a": 1 }); everything is OK again.
Why does this happen?
The statement:
{ a: 1 };
is not an object literal. It's a block statement with one labeled expression in it. It's valid.
This:
{ "a": 1 };
is a syntax error because it's just not parseable. The quoted "a" starts an expression statement inside the block, but then the next token after the string is a colon, and there's no expression form that looks like an expression followed by a colon.
Now:
var x = { "a": 1 };
works because the "{" is not interpreted as the start of a block statement. That statement starts with var, so it's a variable declaration. Within the expression on the right side of the "=" token, the only thing that a "{" can mean is the start of an object literal. Similarly, note that:
({ "a": 1 });
is OK because the opening parenthesis makes the parser expect a nested subexpression, so again the "{" unambiguously means that it's the start of an object literal.
I just realized than when loading the JSON via require and the filename does not end on .json i get this error. Renaming the file to bla.json and it works fine.
This error can popup when doing a jQuery AJAX call using jsonp when jsonp is not necessary. Try switching your data type on your AJAX call if this is the case to normal json
$.ajax({
dataType: 'json', // try using json rather than json p
...
});

Categories