Problem parsing JSON - javascript

Hi got a weird problem parsing JSON for some reason can't access the values contained, can't see that I'm doing anything wrong but guessing i'm doing something silly any help would be greatly appreciated.
Json -
{
"toilClaims": [
{
"id": "1",
"name": "jonathan",
"date": "12/12/2011",
"hours": "8",
"reason": "asdasda"
},
{
"id": "2",
"name": "jonathan",
"date": "12/12/2011",
"hours": "3",
"reason": "sdasdasdasd"
},
{
"id": "3",
"name": "trvor",
"date": "12/2/2",
"hours": "we",
"reason": "asda"
}
]
}
I won't include all js but in a return function from a ajax call (data comes back fine), I simply try the following:
function listSetup(data, refresh) {
console.log(data.toilClaims.id[2]);
}
How ever it fails to find the id, I can however access data.toilClaims ???
Help lol

In this case, toilClaims itself is the array. Try this:
console.log(data.toilClaims[2].id);

As pointed by #dante617, toilClaim is an array, just by looking at square brackets you can tell that its an array or objects.

Related

JSON weird format access data

This is a JSON snip that is created in WooCommerce by a plugin that adds some metadata. I cannot change the formatting of this JSON because it is generated by a plugin. The problem is that the keys and the values are added in a weird way. I am iterating through the line_items and statically referencing this data which I don't want to do, I would like to know if there is a smart way to reference for example:
"key": "_cpo_product_id",
"value": "3572",
if this was formatted correctly it would be: "_cpo_product_id": "3572" and not have "value" as a key, and it would be accessed by: foo.line_items[i]._cpo_product_id
but with this configuration I am a bit lost, I am sure there is an easy way to find the value for a specific key. I am doing this on Google app scripts, but a solution in JavaScript should suffice.
JSON snip:
"line_items": [
{
"id": 749,
"name": "Dune",
"product_id": 3572,
"variation_id": 0,
"quantity": 1,
"tax_class": "",
"subtotal": "149.54",
"subtotal_tax": "31.40",
"total": "149.54",
"total_tax": "31.40",
"taxes": [
{
"id": 24,
"total": "31.403148",
"subtotal": "31.403148"
}
],
"meta_data": [
{
"id": 11919,
"key": "_cpo_product_id",
"value": "3572",
"display_key": "_cpo_product_id",
"display_value": "3572"
},
{
"id": 11920,
"key": "_add-to-cart",
"value": "3572",
"display_key": "_add-to-cart",
"display_value": "3572"
},

Zapier API Code assistance - extract data from JSON

I am trying to use a JSON response in a Zapier response. The data looks like this:
I'm trying to use the Induction and Site Suitability href value in a Zap, however, when I try to use this in Zapier, Zapier gives me this:
I was going to use a find of some sorts to find Induction and Site Suitability and then return the href after that, but because of the formatting, I can't do that. Does anyone know how I can do this with Formatter or Code (Python/Javascript) or something else in Zapier?
Thank you
Luke
Here is the API result in code:
{
"competencies": [
{
"competency": {
"name": "Forklift Operator Permit",
"href": "https://link/22059"
},
"status": {
"value": "Active",
"type": "active"
},
"expiryWarning": "2021-04-15T13:59:00Z",
"expiry": "2021-04-30T13:59:00Z",
"disabled": false,
"href": "https://link/1b1594d738"
},
{
"competency": {
"name": "Induction and Site Suitability",
"href": "https://link/24033"
},
"status": {
"value": "Active",
"type": "active"
},
"expiryWarning": "2021-04-15T13:59:00Z",
"expiry": "2021-04-30T13:59:00Z",
"disabled": false,
"href": "https://link/8e26b5ea4bbc800e"
}
]
}

Nested json data in js -get the values from nested array

Got the output result in json nested array.
Help to access the USER-id of this json format .
var result = {
"USER": {
"id": "11456",
"email": "g#gmail.com",
"name": "g"
},
"status": "true",
"group-title": "title",
"group-name": "2-Group"
}
I thin your json structue is wrong. Below is the corrected structure
{
"status": "true",
"USER": {
"id": "11456",
"name": "g",
"email": "g#‌​gmail.com"
},
"group-name": "2-Group",
"group-title": "title"
}
The json usages in JS
var result={ "status": "true", "USER": { "id": "11456", "name": "g", "email": "g#‌​gmail.com" }, "group-name": "2-Group", "group-title": "title"};
resultJson=jQuery.parseJSON(result);
var userId=resultJson.USER.id; // here you will get the user id
Please try this way. This may help you. Don't forget to add jQuery in your script
json values are not seperated by
;
the correct formate for your json is
var result = {"status":"true","USER":{"id":"11456","name":"g","email":"g#gmail.com"},
"group-name":"2-Group","group-title":"title"}
for this formate u can assess the user id by
result.USER.id;
From your code, you can directly access Id as below:
<script>
var result = {
"USER": {
"id": "11456",
"email": "g#gmail.com",
"name": "g"
},
"status": "true",
"group-title": "title",
"group-name": "2-Group"
};
alert(result.USER.id);
</script>

The same graph is repeated in my view and upon page refresh, the graph changes

This is my loop in the Controller. I am trying to create and display a chart using Angular JS but I am unable to do so. The same graph is repeated in my view and upon page refresh, the graph changes. I want the response "Options" and "Votes" from the JSON. What mistake am I making?
for (var i = 0; i < response.response.options.length;i++) {
labels.push(response.response.options[i].options);
data.push(response.response.options[i].votes);
$scope.labels = labels;
$scope.data = [
data
];
This is my JSON. I have multiple calls and thus multiple JSONs like this are called from the API.
{
"response": {
"poll": {
"id": "17",
"story_id": "28346",
"user_id": "32425",
"question": "Who's your favorite actor?",
"status": "1",
"docs": "1473121969"
},
"options": [
{
"id": "59",
"options": "1. Hamza Ali Abbasi",
"image_url": "31xjr2a6f838gwqkw68p.png",
"doc": "1473121970",
"votes": "3"
},
{
"id": "60",
"options": "2. Emmad Irfani",
"image_url": "w9yhbtlr39pzn9d9bijf.png",
"doc": "1473121970"
},
{
"id": "61",
"options": "3. Shahbaz Shigri",
"image_url": "hxnbem90wtlo7eb8tuew.png",
"doc": "1473122087"
},
{
"id": "62",
"options": "4. Fawad Khan",
"image_url": "f9q6ud83bjt8ateazzzs.png",
"doc": "1473122111"
}
]
}
}
I think I know what you are getting at, with why it isn't working, and that may be your "data" array. Here is a jsfiddle that has it working and displaying the info you want:
https://jsfiddle.net/jwb9acqL/
If you look at the console output of the object in your original code, the output was:
[labels: Array[4], data: Array[1]]
Where the "votes" in data were for some reason put in another subarray:
$scope.data = [
data
];
I removed that extra array. See if that helps. If not, I would say you need to post the graph code.

how to read json format data from localStorage

I created a fiddle here.
How to calculate the currencies values by reading data from the localStorage?
can anyone tel me steps to achieve this target?
i have jason data obtained by calling remote websites as below
{
"list": {
"meta": {
"type": "resource-list",
"start": 0,
"count": 168
},
"resources": [{
"resource": {
"classname": "Quote",
"fields": {
"name": "USD/KRW",
"price": "1062.280029",
"symbol": "KRW=X",
"ts": "1396294510",
"type": "currency",
"utctime": "2014-03-31T19:35:10+0000",
"volume": "0"
}
}
}, {
"resource": {
"classname": "Quote",
"fields": {
"name": "SILVER 1 OZ 999 NY",
"price": "0.050674",
"symbol": "XAG=X",
"ts": "1396287757",
"type": "currency",
"utctime": "2014-03-31T17:42:37+0000",
"volume": "217"
}
}
}
]
}
}
i am actually learning how to use javascript by dealing with localstorages. I am a serverside programmer, this is my 3rd day of javascript programming. Hope someone can help me here
JSON.stringify is a serialization function. The unserialize function is called JSON.parse.
In your code localStorage.setItem('all_currencies',JSON.stringify(d)); this line converts your JSON Object to String so it can store in localStorage
Now for get data back from there you have to write
var jsonStringFromLS = localStorage.getItem('all_currencies');
Now in jsonStringFromLS you have all data in string form to convert into Object pass this value into following function
console.log(JSON.parse(jsonStringFromLS));
from above line you get your data back in JSON object form so you can now process this object.
Demo

Categories

Resources