After calling console.log(JSON.stringify(req.params)), I get a string with the following structure:
{"q":"{\"email\":\"mymail#mail.com\"}","apiKey":"1234"}
With console.log(req.params.q), I have this result: {"email":"mymail#mail.com"}.
But I get "undefined" if I try to view the email value with console.log(req.params.q.email) or console.log(req.params.q["email"])
What is the best approach to get that value?
You must JSON.parse that inner part :
var test = {"q":"{\"email\":\"mymail#mail.com\"}","apiKey":"1234"};
alert(JSON.parse(test.q).email);
alerts mymail#mail.com
Why?
Because test holds an javascript object where q holds a string, So you must parse that string if you want to extract the JSON values from that string.
It looks like req.params.q is a string: "{\"email\":\"mymail#mail.com\"}".
You need to parse that json then fetch the value.
req = {params: {"q":"{\"email\":\"mymail#mail.com\"}","apiKey":"1234"}}
JSON.parse(req.params.q)
> Object {email: "mymail#mail.com"}
JSON.parse(req.params.q).email
> "mymail#mail.com"
Related
How do I access the value stored in the description property using javascript?
As seen in the image below (a screenshot from the browser console), the stored value is [STK_CB - ] Request Cancelled by user
In the browser console, I have tried console.log(responseMan.payload["0"].jsonPayload.description); which shows undefined. Where am I going wrong?
Looking forward to your help.
The value of jsonPayload is a string, not an object -- notice the double quotes around it. And the name of the property implies that it's JSON. You need to call JSON.parse() to convert it to an object.
var payload = JSON.parse(responseMan.payload[0].jsonPayload);
console.log(payload.description);
just remove " from index and do it like:
u also have to cast the json first
const jsonStr = responseMan.payload[0].jsonPayload;
const data = JSON.parse(jsonStr);
console.log(data.description);
The problem is in your json. Because you are trying to access responseMan.payload ["0"]. JsonPayload up there is a correct element in the json. But the description is a string value in responseMan.payload ["0"]. JsonPayload, therefore, must format the contents of responseMan.payload ["0"]. JsonPayload on a json object. Example var obj = JSON.parse (responseMan.payload ["0"]. JsonPayload); and so
description will be a json object
OR
You can fix that json in your backend and send that section as a json and not as a string
I'm trying to replace the angularjs POST query with standalone JSON response string.
When angular GET / POST queries returns a response automatically converted to JSON and the code was working like charm.
Now, I'm trying to have the json response stored as a javascript string variable in the controller and then trying to parse it using JSON.stringify() and subsequently using JSON.parse().
There is no error but the resulting json object's member variables can't be accessed using the . operator
var staticData = '{"someKey":"someValue", "masterJobs":[]}'; //very large json string.
var resultString = JSON.stringify(staticData);
$scope.staticTestData = JSON.parse(resultString);
console.log($scope.staticTestData.masterJobs); // this displays 'undefined'
Controller function with the large JSON is available here.
You already have a string, so there is no need to use JSON.stringify.
Just use the following code:
var staticData = '{"someKey":"someValue", "masterJobs":[]}'; //very large json string.
$scope.staticTestData = JSON.parse(staticData);
console.log($scope.staticTestData.masterJobs);
{
"content": "{\"text\":\"Executing NodeDatasetFileOrDirectoryCSV : 1\",\"id\":1,\"name\":\"CSV\",\"type\":\"text\"}"
}
\ tag is getting appended after everything.
I want to access the type field. But i am not able to even after content.type because of the \ appended after every element. How to remove this ?
You're response is coming down as a valid JSON object, but the content property holds a value that is a JSON string, not a JSON object. You can either fix it on your server-side however you are constructing your response, or you can use JSON.parse to parse the content JSON string into a full-fledged object in JavaScript after you get your response.
The latter would be something like this:
var response = {"content": "{\"text\":\"Executing NodeDatasetFileOrDirectoryCSV : 1\",\"id\":1,\"name\":\"CSV\",\"type\":\"text\"}" };
response.content = JSON.parse(response.content);
console.log(response.content.type);
Use JSON.parse() to get the JSON object from the string and then get the values using keys.
I have some JSON Data like this:
[{"2015-02-10": ["00:00","00:30","01:00","01:30","02:00","02:30","03:00","03:30","04:00","04:30","05:00","05:30","06:00","06:30","07:00","07:30","12-00","12-30","13-00","13-30","18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30","22:00","22:30","23:00","23:30"]},{"2015-02-17": ["00:00","00:30","01:00","01:30","02:00","02:30","03:00","03:30","04:00","04:30","05:00","05:30","06:00","06:30","07:00","07:30","12-00","12-30","13-00","13-30","18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30","22:00","22:30","23:00","23:30"]}]
Now I parse it in Javascript and tried to access the values of a specific date:
var hours = $.parseJSON('JSON STRING ...');
var date = "2015-02-10";
console.log(hours[date]);
But I always get "undefined" and I really don't know how I can access this
Your data is an array ([ ... ]) with an object inside it.
Try:
console.log(hours[0][date]);
My web service returned a JSON Array (ie. [{"key":"value"}, {"key":"value2"}]). In the array there are two items as you can see, which are separated with comma. I want to know how can I access the second item, and get the value of "key" for the second item.
I've tried:
var a = msg.d[1].key
With no success of course.
This is the returned string:
"[{"Code":"000000","Name":"Black","Id":9},{"Code":"BF2C2C","Name":"Red","Id":11}]"
The string was extracted using FireBug after watching the msg.d.
Need your help in solving this.
msg[1].key
Assuming that the name of that array is msg. I'm not sure what you are using .d for.
If msg.d is a string representing an array, use JSON.parse.
JSON.parse(msg.d)[1].key
You can replace key with the key you are wanting, e.g. Code, Name, Id, etc.
This works as expected for me.
var msg = [{"key":"value"}, {"key":"value2"}];
var a = msg[1].key;
What is msg in the example above? Need more info to help.
If msg.d is a string then you have to eval (uggh) or parse it before applying the array subscript.