Access JSON Object data with space in name [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I make an API call and get returned some JSON. I then parse this JSON using
var json = $.parseJSON(result);
To get to the level I need to within this json object, I do something like this
console.log(json.data[0].value)
That will print something like the following to the console
Option "1166325"
Option Two "3329076"
So if I do
console.log(json.data[0].value.Option)
I am printed out 1166325. If however I do
console.log(json.data[0].value.Option Two)
I get an error SyntaxError: missing ) after argument list. I have also tried
console.log(json.data[0].value.['Option Two'])
But this returns SyntaxError: missing name after . operator
So how can I access this data considering there is a space in the name?
Thanks

Yes, just lose the . character:
console.log(json.data[0].value['Option Two'])
This is known as bracket notation and can always be used to access a property. Dot notation can only be used when the property name is a valid Javascript identifier. This means (a) letters and numbers; (b) _ underscores; (c) $ dollar signs.

Related

How to pass a single quote around a javascript variable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
I have a javascript code as shown below in which I want to pass a single quote around a javascript variable attribute.
Problem Statement:
I am wondering what changes I need to make at Line A so that I am able to pass a javascript variable in a single quote.
At Line A, I did this `'${attribute}'` but I am getting an error Uncaught SyntaxError: missing ) after argument list.
It looks like you're trying to use a template literal, but have over-complicated it:
confirm(`Are you sure you want to delete '${attribute}' ?`)
Note the use of back-ticks around the whole template literal, which syntactically allows you to use any quotes you like within the literal itself.
You need to put the whole string inside backticks and then you don't have to 'escape' your single quotes anymore.
confirm(`Are you sure you want to delete '${attribute}' ?`)

Uncaught SyntaxError: expected expression, got ';' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
var popupContent = 'Click me!';
I'm currently using Leaflet and want a popup with clickable text, which will run some Javascript to define the variables stopno and nuscode. However, in the GeoJSON data feature.properties.nuscode is a string and not an integer, and thus I need to use quote marks to define the variable nuscode.
However, if I were to use quote marks again it would cause issues as I am using inline JS in the onclick property for the <a> tag, and using quote marks causes "Unexpected end of input" errors. How do I remedy this?
As of ES2015, you could use backticks to create what is known as template literals (template strings).
You can encapsulate text in backticks and interpolate JavaScript so that your code is much neater. It also helps avoid any conflicting use of single/double quotation marks, and avoids having to escape (\) strings.
More can be read about it here on MDN
const popupContent = `Click me!`;

JSON.stringify converts colon inside string to a unicode character [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I have a string object in javascript as given below:
time : "YYYY-MM-DDT00:00:00.000Z#YYYY-MM-DDT23:59:59.999Z"
When I use JSON.stringify to convert the object to string, I get the following string
"time=YYYY-MM-DDT00%3A00%3A00.000Z%40YYYY-MM-DDT23%3A59%3A59.999Z"
Here 2 characters, i.e., # and : are being converted to their unicode, which is unwanted behaviour.
How can I prevent this unwanted conversion, so that my string remains unchanged?
I can't reproduce your problem, can you show the complete code where the error occurs.
Also, try doing this conversion on the browser console and see what the result comes out to be.

JSON parse error with double quote as unrecognized token [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I've been stuck hours on this supposedly dummy problem.
I am getting data from an hardware device via bluetooth, and after decoding it with :
const int8View = new Uint8Array(data)
const decoder = new TextDecoder('utf-8')
const toParse = decoder.decode(int8View)
I can get a json string that i can print:
console.log("should be parsed ", toParse)
-> should be parsed – "{\"timeStamp\":1580011546,\"startState\":2,\"endState\":3}"
but when I use JSON.parse on this object I get:
SyntaxError: JSON Parse error: Unrecognized token ''
As often when you take time to clearly explain your problem, you find the solution. As I have found nothing on the matter, I give my answer.
If you copy paste the double quote of the error message in an hidden character view like this one it will show you that there was an hidden null \x00 character inside the string.
I removed it with:
toParse.toString().trim().replace(/\0/g, '')
which solved my problem.
Hope this helps others.

Uncaught SyntaxError: Unexpected string error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm getting an Uncaught SyntaxError: Unexpected string error, yet when I look at the source, as far as I can tell, it's correct. I can't see any missing commas or quotation marks. Any one have any tips?
(yes, I've searched to see if I can find the problem, but it always seems to be quotation marks and commas)
Thanks
You need to add a pair of square brackets to contain the entries
For example,
let StandardsData = [
{
// stuff
},
{
// stuff
},
{
// stuff
},
]
You missed to close between [ ] that array

Categories

Resources