Parse value only from JSON string [duplicate] - javascript

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 9 months ago.
An API returns a string of text that looks like this (xxx used for security):
{"xxx":{"xxx":{"xxx":{"xxx":{"results":[{"latest.GigabytesIngested":12641.824682336}]}}}}}
If I do this:
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]);
I get this, which is fine:
{ 'latest.GigabytesIngested': 12641.82487968 }
My problem is I only want to grab the number. The below attempt doesn't work, maybe because there's a dot in the key name, or maybe because I'm just doing it wrong?
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0].latest.GigabytesIngested);

#derpirscher answered correctly in a comment:
console.log(JSON.parse(body).data.actor.account.nrql.results[0]['latest.GigabytesIngested']);

Yes, the period in the key is the problem. You need to use an alternate way to reference the key.
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]["latest.GigabytesIngested"]);
or
var result = JSON.parse(body).xxx.xxx.xxx.xxx.results[0];
var lgi = result["latest.GigabytesIngested"];
console.log(lgi);

Related

How to skip the - character inside a JSON field in JS/type script [duplicate]

This question already has answers here:
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 2 years ago.
One of the JSON field in my JSON file has the field "AlphaWorkStatusChangeInfo-Comment" and I'm trying to add value to that field. But some how the type/JS script is not accepting the "-" in the field.
let data = JSON.parse(JSON.stringify(Agri.data));
data.AlphaWorkStatusChangeInfo = ["other"];
data.AlphaWorkStatusChangeInfo-Comment = "Had to quit the job";
data.autoSave = true;
What are my options to include/Add the value in the field "AlphaWorkStatusChangeInfo-Comment".
Any help is much appreciated.
The hyphen is not valid unless you encase it as a string element like so:
data["AlphaWorkStatusChangeInfo-Comment"] = "Had to quit the job";
See here for more discussion on this topic: Which characters are valid/invalid in a JSON key name?

Simple format of text on front-end of my website (replace characters)? [duplicate]

This question already has answers here:
Remove first character from a string if it is a comma
(4 answers)
Closed 3 years ago.
My website uses product references in this style: "R202020"
I want them to be shown like this for the users of my website: "BA2202020"
So basically I'm looking for a script, which formats the style of my reference numbers (should affect a ".reference" class I've created) by:
Removing the "R" in the original reference - replacing it with a "BA2" in stead - leaving the rest as it is (the "202020" part).
How can I do this?
Find 1st character of your string using string[0] and replace that with your desire value like below.
var string=$('.YourClass').text();
var result = string.replace(string[0],'BA2');
$('.YourClass').text(result);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class='YourClass'>R202020</span>
Try replace method. https://www.w3schools.com/jsref/jsref_replace.asp
'R202020'.replace('R2','BA2') // BA202020

How to read data between xml tags with attributes in javascript/ionic without using DOM/ActiveXObject? [duplicate]

This question already has answers here:
Parse XML using JavaScript [duplicate]
(2 answers)
Closed 5 years ago.
I simply need to extract some info between two tags, in this case, <wsse:BinarySecurityToken>
For example:
<wsse:BinarySecurityToken att1="abc" att2="cd3" att3="adfa">This is a text I need!!!===</wsse:BinarySecurityToken>
I tried
match = text.match(/<wsse:BinarySecurityToken[.*]>([^<]*)<\/wsse:BinarySecurityToken>/g)
does't work!
Or is there anything better than regex? I use angularJs 1
You want to do:
match = text.match(/<tag1.*>([^<]*)<\/tag1>/g)

Modify Javascript Variable with URL [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 6 years ago.
Is it possible to change a javascript variable through the URL?
Here's an example of the code I'm trying to modify from a website. (www.example.com)
<script type="text/javascript">
var x = 0;
</script>
I want to change the variable x from 0 to 1.
I want to do this by appending something to the URL. I'm not sure about the syntax, but I think it may be something like this:
www.example.com#javascript: var=1;
Is it possible to change variable x by only modifying the URL?
EDIT:
The duplicate question doesn't tell me how (if it's possible) to change the variable through the URL. Please let me know if that's not the case.
Related Question:
https://security.stackexchange.com/questions/134240/modify-javascript-variable-with-url-exploit
you can use:
if(window.location.href.indexOf("your_link_to_check") > -1) {
var x = 1;
}

How to add to an array in javascript? [duplicate]

This question already has answers here:
How to append something to an array?
(30 answers)
Closed 6 years ago.
is anyone able to tell me what i am doing wrong? The problem is that "ramdom1" is not being added to the array.(nothing is showing up in the text_loot)
Any help would be great, thanks.
var lootArray = [].join("<br>");
lootArray.add("ramdom1");
document.getElementById("text_loot").innerHTML = lootArray;
First, the method to add element to array is called push, not add
Second, when you do join on array, you get string, which doesn't have methods like push or add
If you really want to do this, you need this code:
var lootArray = []
lootArray.push("ramdom1");
lootArray = lootArray.join("<br>");
document.getElementById("text_loot").innerHTML = lootArray;
Replace
lootArray.add("ramdom1");
with
lootArray.push("ramdom1");
use push method like this. hope it will works
lootArray.push("ramdom1");

Categories