I'm getting the following JSON response:
"Nitro": {
"Login": {
"sessionKey": "NHwxNDQ4MzR8MzY1ODE5ODMwMnwxMzQ0NzE5MTYyfDgxY2M1NjYxZDBiY2NiODI4NmM2Mjc1ODI2MzA1NDY3YmVhNzJjZDR8MA=="
},
"res": "ok",
"method": "user.login",
"server": "sbnitro01.prod.bunchball.net/nitro4.4.0"
}
}
I don't understand how to parse this with JavaScript and store the value of the sessionKey in a variable. I cannot use jQuery on this project and just cannot seem to get the key with JS.
Thanks!
Try:
var sessionKey = obj.Nitro.Login['sessionKey'];
This takes the object that is stored in the obj variable, then takes the Nitro property object and then takes the Login property object and extracts the property sessionKey accordingly.
Related
I'm making an ajax post request to a super simple python function that takes a student's name and spits out a url that corresponds to it. Currently, the Python function passes this back in json and looks like so when console.log(JSON.stringify(response)) is called:
{"readyState":4,"responseText”:”\ {\”studentURL\”: \”https://prepacademy.jackjohnson.com\”} ”,”responseJSON”: {“studentURL”:”https://prepacademy.jackjohnson.com”},”status":200,"statusText":"OK"}
I was wondering how do I take this larger chunk of information and filter it so that I would only get the https://prepacademy.jackjohnson.com part?
response is a JavaScript Object of which you can access the properties using either Dot-notation or bracket-notation, like so:
let response = {
"readyState": 4,
"responseText": "\ {\"studentURL\": \"https://prepacademy.jackjohnson.com\"} ",
"responseJSON": {
"studentURL": "https://prepacademy.jackjohnson.com"
},
"status": 200,
"statusText": "OK"
};
// dot-notation
console.log(response.responseJSON.studentURL)
// bracket-notation (allows for computed paths)
console.log(response["responseJSON"]["studentURL"])
response.responseJSON.studentURL
I have been working on this problem for quite a while now.
I am defining an array using the following:
let newJsonObject = {
"billing_name": document.getElementsByName("order[billing_name]")[0].value,
"email": document.getElementsByName("order[email]")[0].value,
};
I get the data from storage and keep it in a variable named parsedJson, then do the three following console.log operations:
console.log(parsedJson);
console.log(parsedJson.billing_name);
console.log(parsedJson["billing_name"]);
This first returns an object with the following:
parameters:
{"billing_name": "123", "email": "123"}
However, the following two things logged in the console are undefined.
I have also tried to create the object with the keys not having quotations, but I am still getting undefined
I don't understand why the log is returning null when the object is defined. Does anyone have any suggestions?
EDIT:
Here is how I am storing the data:
chrome.storage.sync.set({"parameters": JSON.stringify(newJsonObject)});
Here is how I am accessing it:
chrome.storage.sync.get("parameters", params => {
if(params === null){
//Nothing is even set, simply return
return;
}else{
//Actually data saved in params
let parsedJson = params;
console.log(parsedJson);
console.log(parsedJson.parameters.billing_name);
console.log(parsedJson["billing_name"]);
Here is a link to what is displayed in console
use JSON.parse converts your json string to object
try this
console.log(JSON.parse(parsedJson.parameters).billing_name);
Try this:
var test = JSON.parse(parsedJson.parameters);
console.log(test.billing_name);
this will work.
I am getting undefined error when trying to assign a value from my ajax retrieved local json file. I can alert and console.log the data just fine but I get an error when trying to assign a value to a variable. typeof returns "undefined".
JSON:
[
{
"name": "fhtyhtfht",
"website": "fthfthfth",
"description": "trgrgfthyg",
"type": "marker",
"coordinates": [
54.637465,
-8.440456
]
},
...
]
Json is retrieved with ajax and the returned variable is of type object. I am able to retrieve string values with data.name for example but coordinated is undefined.
console.log(typeof data.coordinates); // undefined
console.log(typeof data.coordinates[0]); // Uncaught TypeError: Cannot read property '0' of undefined
Edit: The issue frustratingly was caused because I defined a variable with the same name as the method property, coincidentally many properties existed in both variables so the issue only arose with one property.
Use:
console.log(typeof config == "undefined")
or even
try {
console.log(config)
} catch(e){
console.log(e)
}
Good evening,
I'm trying to save an associative array into chrome.storage.local, like so:
var keyName = 'name';
var data = //grabbed from an Ajax call
saveData(keyName, data);
function saveData(keyName, data){
console.log("saving with key: "+keyName);
chrome.storage.local.set({keyName:data});
}
To check to make sure the data saved properly, I load:
function loadData(keyName){
console.log("loading: "+keyName);
chrome.storage.local.get(keyName, function(result){
console.log(result);
});
}
The log shows it is trying to load the correct key name, but nothing comes up. I then try calling loadData(null), which will show the entire contents of the local storage, and I find:
Object {keyName: Array[3]}
keyName: Array[3]
__proto__: Object
My data! But the key it saved with is "keyName" instead of "name". The log from saveData outputs that it is "saving with key 'name'", but it's saving with key "keyName" instead...
????
Thanks!
How strange...
Seems my question is similar to Using a variable key in chrome.storage.local.set
The answer they found was to convert the JSON {keyName:data} to an object:
var obj = {};
obj[keyName] = data;
chrome.storage.local.set(obj);
This works.
Is this because the JSON field is automatically passing as a string?
I have a input type file where I save the file to a file object like so
var uploadControl = document.getElementById("fileUpload");
var files = uploadControl.files[0];
Then I would like to save that file to local storage to read into the FileReader object at a later time. Does anyone know if this is possible. I have tried several different options and every time I try and retrieve the object out of local storage, it is undefined.
Here are some things I have tried
var setObj = {"prop": "myProp", "file": files};
chrome.storage.sync.set({"myObj":setObj});
This doesn't throw errors, but when I try and retrieve the object, it is undefined
chrome.storage.sync.get("myObj", function(item) {
console.log("item name: " + item.myObj.file.name);
});
However I can access the other properties of the object
chrome.storage.sync.get("myObj", function(item) {
console.log("item prop: " + item.myObj.prop);
});
Am I doing something wrong when adding the object to local storage or am I accessing it incorrectly? Or is it just impossible to do this?
localStorage can only contain string name/value pairs, which means you can not store an object directly.You can use stringify and parse for that.