JSON weird format access data - javascript

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"
},

Related

How to make JavaScript object from the given json string

I have a large json data that I have to parse and get object out of the following Json string. I added just top part of that large json data. I tried to parse it by
let obj = JSON.parse(this.state.data);
it doesn't work, it breaks with this msg "SyntaxError: Unexpected token L in JSON at position 0".
If I get the console.log by console.log(JSON.stringify(this.state.data, null, 2)); and try to validate output by online JSON validator, it says it is valid JSON data. Could you please tell me how can I parse it?
{
"content": [
{
"_id": 1,
"name": "Warehouse A",
"location": {
"lat": 47.13111,
"long": -61.54801
},
"cars": {
"location": "West wing",
"vehicles": [
{
"model": "GX",
"price": 27395.26,
"licensed": false,
"_id": 15,
"make": "Lexus",
"year_model": 2005,
"date_added": "2017-11-12T00:00:00.000+00:00"
},
{
"model": "Q",
"price": 6103.4,
"licensed": false,
"_id": 9,
"make": "Infiniti",
"year_model": 1995,
"date_added": "2017-11-13T00:00:00.000+00:00"
},
.........xxxxxx continue
Your variable this.state.data is not a JSON. It's a Javascript Object, just like JSON, So, Why not just use let obj = this.state.data;

ExtJS data model unknown amount of fields

I have a data model that attempts to map all the incoming data, however I have to leave the data generic so i can add any number of meta data tags to my project. How can I either map these data fields to the Model or access these items in the array when applying grid filters.
{
"Link": "link.com",
"Title": "project",
"Description": "descript",
"State": "TN",
"Metadata": [
{
"Name": "County",
"Value": "32"
},
{
"Name": "Info",
"Value": "info"
},
{
"Name": "State",
"Value": "TN"
}
],
"XMin": "-1",
"XMax": "-1",
"YMin": "1",
"YMax": "1"
}
I think this is what you are looking for https://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.association.HasMany
There is also nice explanation about filtering on that page

Jquery dataTable handle json data with pagination

First, I have data from url like this GET /api/fruit?per_page=10&page=1 and this is what I got :
{
"data": [
{
"id": "1",
"name": "Orange",
"price": "200"
},
{
"id": "2",
"name": "Manggo",
"price": "300"
},
...(display 10 data)
],
"meta": {
"pagination": {
"total": 69487,
"count": 10,
"per_page": 10,
"current_page": 1,
"total_pages": 4633
}
}
}
Then I following this code fiddle and success render data, and pagination work as well. But input search not working. My question is how to use jquery dataTable properly with data from api like this GET /api/fruit?per_page=10&page=1

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

Backbone , changing models attributes

In a collection i need to set the value to one of attributes, but i cant find the way how do it
products.models[i].set({'category.name':'some_value'})
the rest api looks like this
{
"category": {
"id": 3,
"name": "Drink",
"icon": "staging/main/category/icon-drinks.png"
},
"id": 1,
"name": "Sugar54",
"dashboard": 1,
"last_buy": "2013-10-02",
"price": "102",
"buy_period": 7
},
How do i do that ?
If you have complex nested models, I suggest you take a look at BackboneRelational.
Otherwise in your case, you should be fine with
products.models[i].get('category').name = 'some_value';
supposing that category is a normal object.

Categories

Resources