This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 3 months ago.
I am unable to retrieve a value from a json object when the string has a dash character:
{
"profile-id":1234, "user_id":6789
}
If I try to reference the parsed jsonObj.profile-id it returns ReferenceError: "id" is not defined but jsonObj.user_id will return 6789
I don't have a way to modify the values being returned by the external api call and trying to parse the returned string in order to remove dashes will ruin urls, etc., that are passed as well. Help?
jsonObj.profile-id is a subtraction expression (i.e. jsonObj.profile - id).
To access a key that contains characters that cannot appear in an identifier, use brackets:
jsonObj["profile-id"]
In addition to this answer, note that in Node.js if you access JSON with the array syntax [] all nested JSON keys should follow that syntax
This is the wrong way
json.first.second.third['comment']
and will will give you the 'undefined' error.
This is the correct way
json['first']['second']['third']['comment']
For ansible, and using hyphen, this worked for me:
- name: free-ud-ssd-space-in-percent
debug:
var: clusterInfo.json.content["free-ud-ssd-space-in-percent"]
For anyone trying to apply the accepted solution to HomeAssistant value templates, you must use single quotes if you are nesting in doubles:
value_template: "{{ value_json['internet-computer'].usd }}"
If you are in Linux, try using the following template to print JSON value which contains dashes '-'
jq '.["value-with-dash"]'
It worked for me.
Related
This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 2 years ago.
Please note that this is not a json object :) My string is:
{"message":"***error in SAP module:-1***","status":400}
This is not a json object, this is a pure string. I cannot turn it into a json object due to technical limitations.
So, I want to take only the bold string (all the value of "message").
I thought about lastIndexOf, and pick the string between ":" and ","
But I got messed up with the escape characters for the quotes.
How can I achieve it with lastIndexOf? Or in another better way?
If you can't use JSON.parse, I would use a regex to read it. You know you want the string directly following "message":", so I would look for that, then grab everything from there to the next occurrence of "
This question already has answers here:
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 4 years ago.
I have some code which is giving me the following error when I rename a class name.
This gives no error:
this.container = document.createElement("ul"),
But when I rename the container class name to this:
this.tt-container = document.createElement("ul"),
I get the following error:
Syntax error: Invalid left-hand side in assignment expression
How can I fix this?
use _ instead of -, so this.tt_container, not this.tt-container
From MDN,
An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. However, any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation.
In your case - is not a valid JavaScript identifier. To use -, Use Bracket Notation
this["tt-container"] = document.createElement("ul"),
This question already has answers here:
What do the brackets around the arguments mean when reading documentation for a method? [duplicate]
(3 answers)
Closed 4 years ago.
I am using criso validator.js, of user Input,
but it Eslint is showing error in syntax on this lines
if (!Validator.isAlphanumeric([(data.password,'en-US')])) {
console.log(" Not an alphanumeric");
}
how to properly check user's entered password is Alphanumeric,
I know we can do it using regex but I wanted to do it by using their provided syntax as isAlphanumeric(str [, locale]).
here is their documentation screenshot of code.
The square brackets in the isAlphanumeric(str [, locale]) notation are not related to JavaScript Array literals. Instead, they denote that when calling isAlphanumeric, the first argument str (in your case, data.password) is required, and the second argument locale is optional.
In your case, you do want to pass in a locale. Here is how that would look:
if (!Validator.isAlphanumeric(data.password, 'en-US')) {
console.log("Not an alphanumeric");
}
In technical documentation, square brackets ([]) generally denote that an argument is optional. Tecnhnically this is just a convention (and probably comes from Unix CLI Usage Messages), but in my experience is so widely used that always interpreting square brackets as denoting an optional argument is usually a safe assumption to make.
This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 3 months ago.
I am unable to retrieve a value from a json object when the string has a dash character:
{
"profile-id":1234, "user_id":6789
}
If I try to reference the parsed jsonObj.profile-id it returns ReferenceError: "id" is not defined but jsonObj.user_id will return 6789
I don't have a way to modify the values being returned by the external api call and trying to parse the returned string in order to remove dashes will ruin urls, etc., that are passed as well. Help?
jsonObj.profile-id is a subtraction expression (i.e. jsonObj.profile - id).
To access a key that contains characters that cannot appear in an identifier, use brackets:
jsonObj["profile-id"]
In addition to this answer, note that in Node.js if you access JSON with the array syntax [] all nested JSON keys should follow that syntax
This is the wrong way
json.first.second.third['comment']
and will will give you the 'undefined' error.
This is the correct way
json['first']['second']['third']['comment']
For ansible, and using hyphen, this worked for me:
- name: free-ud-ssd-space-in-percent
debug:
var: clusterInfo.json.content["free-ud-ssd-space-in-percent"]
For anyone trying to apply the accepted solution to HomeAssistant value templates, you must use single quotes if you are nesting in doubles:
value_template: "{{ value_json['internet-computer'].usd }}"
If you are in Linux, try using the following template to print JSON value which contains dashes '-'
jq '.["value-with-dash"]'
It worked for me.
This question already has answers here:
Babel error: JSX value should be either an expression or a quoted JSX text
(3 answers)
Closed 6 years ago.
I have a bunch of input elements to which I want to assign unique ids.
However, the following string interpolation (i is the mapped element index)
id = `input-add-${i}`
returns
Parsing error: JSX value should be either an expression or a quoted JSX text
I use interpolation successfully elsewhere, so I don't understand what I am doing wrong here.
Ah, my bad. I forgot the curly brackets.
This works
{ id={`input-add-${i}`} }
and this would also work
id={'input-add-' + i}