Bad character in json response angular 2 - javascript

I've retrieved a JSON response from an external API and one of the variable names begins with a # character, so the JSON variable looks like this #text. Unfortunately angular doesn't know how to handle this variable, is there any way delete the # so that the data assigned to the identifier is usable.

In order to reference a property that does not follow the rules for a properly formatted identifier, you must use bracket notation instead of dot notation for the property accessor:
var object = JSON.parse('{"#text":"https://lastfm-im...png","size":"extralarge"}')
console.log(object['#text'])

you cam modifide the response - loop throw it wite map- and chenge the object key -see here:
JavaScript: Object Rename Key

You can use square bracket notation instead of dot notation. like (Object mutation is highly not recommended)
//to access it u can use square bracket notation like k["objectname"]
let k = {"#test":"some data"}
alert("data is "+k["#test"])

Related

How to call function using bracket notation in node js

I know we can access data using dot notation and bracket notation but here if I access faker data using bracket notation I can't access that data. Is there any way to access data using bracket notation?
var faker = require("faker");
console.log(faker.name.findName()); // Output: any name.
console.log(faker["name.findName"]()); // Output: TypeError: faker.name.findName is not a function
The problem is that
faker["name.findName"]
would try to access a property called "name.findName" on the faker-object.
In order to access findName using bracket-notation, you need to change it to:
console.log(faker["name"]["findName"]());
You shouldn't use bracket notation to return functions.
console.log(faker["name"].findName())
or if you really just want to use brackets, try
let _findName = faker["name"]["findName"];
console.log(_findName());

Mapping a dot notation key in an object?

I have an object like this:
let object = {
seniority: 'director_level',
'address.long_form': 'Phoenix, AZ, United States'
}
when I go to print address.long_form like this I get an error.
console.log(object.address.long_form)
How do get the value for 'address.long_form'?
Since address.long_form is in string, you cannot use dot notation instead use this
console.log(object['address.long_form'])
It will work like this: object["address.long_form"], but just don't use dots in object indexes.
You can still extract it but you can't use the literal notation, you need to use the [] notation in order to get past the ambiguity.
i.e.
console.log(object['address.long_form'])
Of course better would to just avoid keys in that form, but depends on your data.

Point the right key with a variable of a json array

I get the following json array from an api:
response = {"base":"USD","date":"2015-11-05","rates":
{"AUD":1.3997,"BGN":1.7971,"BRL":3.8008}}
I get that from the following http query:
$http.get(url).success(function(response){
let assume that
quote = "AUD";
How can I point to the AUD value of rates in response (i.e. rate = 1.3997)?
$scope.rate = response.rates.quote;
does not work...
this is called accessing property value of an object this is how we do it
var quote = 'AUD'
var response = {"base":"USD","date":"2015-11-05","rates":
{"AUD":1.3997,"BGN":1.7971,"BRL":3.8008}}
object = JSON.parse(JSON.stringify(response))
document.write(object.rates[quote])
If you use
rates.quote
This means that quote is a property of rates object which is not;
The value of quote is the property of rates
Try like this
$scope.rate = response.rates[quote];
This is one of those times when you have to use the square bracket notation instead of dot notation even though they are usually interchangeable. You can only use dot notation when you know the real name of the property. When you are using a variable as a placeholder you have to use square brackets.
Dot notaion has its limit, use brackets as others already suggested.
The dot notation is:
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. This notation is also very useful when property names are to be dynamically determined (when the property name is not determined until runtime)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

Access JSON object to get Resource Bundle key/value pair

I know how to access key value pair from JSON object but in my case, the resource bundle keys are mapped to values.
e.g.
var json = {"label.name.first":"foo","label.name.second":"bar"};
Here json.label.name.first doesn't give me "foo".
Can someone help me with this?
Due to using the period character (.) in the key name, you need to use the [] notation to access its value.
console.log( json['label.name.first'] );
Additionally, you have a JavaScript object, not JSON.
The difference between a JavaScript object or JSON is that JSON is always a string. Secondly, JavaScript objects don't require the same quote standards on the key names.
If you just consider the string below, then yes it can be considred JSON (this is why if you paste it into a JSON parser, it tells you it's valid JSON):
{"label.name.first":"foo","label.name.second":"bar"}
However, if you assign that directly to a JavaScript variable then you have a JavaScript object literal, not JSON. This is because JSON is also a valid JavaScript object/array literal when it is not contained in a string:
var obj = {"label.name.first":"foo","label.name.second":"bar"};
If you were to use it as a string, then it is JSON:
var json = '{"label.name.first":"foo","label.name.second":"bar"}';
// json is a string, so it's JSON
var obj = JSON.parse(json); // parse the JSON into an object
The confusion is quote common because the JSON format is very similar to the format of JavaScript object and array literals.
Do this:
json["label.name.first"]
However, I think you are misunderstanding the . notation.
And BTW your json isn't a JSON, it is a javascript object and not its notation.
It's json["label.name.first"] that would get you "foo". Since your property's name contains characters that cannot be used in variable names.
If you're expecting to access these properties using the syntax json.label.name.first then your JSON needs to be:
var json = {
"label":{
"name":{
"first":"foo",
"second":"bar"
}
}
}
This is not right way to create object, you should create one like this.
var json={"label":{"name":{"first":"foo","second":"bar"}}};
it will also work as json string

Get JSON attribute value by paasing attribute name via "var" in Javascript

In json can we get attribute value by passing variably value . Means
It works for me when "name" attribute exists in my "returnData" json object
// It works
var getColValue= returnedData[0].name
but it give undefined error
// It Not works
var refVar ="name";
var getColValue= returnedData[0].refVar;
var getColValue= returnedData[refVar];
should work. Please give it a try.
Use square bracket notation:
returnedData[refVar];
In other words, these two are basically equivalent:
returnedData["name"] === returnedData.name
Note that, using square-bracket notation allows you to set/get property names that wouldn't be valid with the dot notation. Eg, returnedData.some-prop is not a valid Javascript object, but returnedData["some-prop"] is.

Categories