json - How to properly create json array - javascript

Im new to JSON and I have to deal with a complex one.
Please see image below:
It has an error:
I don't know how to properly separate the 2 json arrays. I even tried to use : instead of , on line 18 but I still get errors. BTW, I use http://jsonlint.com/ to validate.

On line 2 you gave a key, but failed to do so on line 19. You have to keep the structure.
Remove the key on line 2, they shouldn't be used for arrays in that way.
Edit: In addition, you are trying to put arrays right in objects, switch the opening and ending object marks ({}) with ([]) for arrays on your first and last line.

[
[
{...},
{...},
...
{...}
],
[
{...},
{...},
...
{...}
],
...
[
{...},
{...},
...
{...}
]
]

I believe the correct way to build this JSON should be:
{
"glEntries": [
{
"generalLedgerId":1,
"accountId": 34,
"amount" : 32334.23,
"descripction": "desc1",
"debit" : "Yes"
},
{
"generalLedgerId":2,
"accountId": 35,
"amount" : 323.23,
"descripction": "desc",
"debit" : "Yes"
},
...
]
}

There are many ways to construct JSON data, but it depends on your data and the way you want to present it. Here are a couple examples - hope it helps:
{
"glEntries": [
{
"object1-prop1": "one"
},
{
"object2-prop1": 1,
"object2-prop2": "two"
},
{
"object3-prop1": [
"a",
"r",
"r",
"a",
"y"
],
"object3-prop1.1": "string"
}
],
"otherEntries": [
{
"objectx": "x"
},
{
"objecty": "y"
},
{
"objectz": [
1,
2,
3,
4
]
}
],
"oneEntry": "json"
}
Other Example:
[
{
"obj1-prop": 222
},
{
"obj2-prop": "object2"
},
{
"obj3-prop": "Object3"
},
[
"a",
"r",
"r",
"a",
"y",
777,
888
],
"string",
178,
{
"objectProp": "testing123"
}
]

You have more {} than needed and will make parsing your JSON more difficult:
Structure will work a lot better like this:
{"glentries":[
{ "property1":"value", "property2" : "value",..... "lastProperty": "value"},
{ "property1":"value", "property2" : "value",..... "lastProperty": "value"},
{ "property1":"value", "property2" : "value",..... "lastProperty": "value"}
]
}
Now glentries is an array of objects that have multiple properties to them.
alert( glentries[0].property2 )

The parent structure is an Object, so it is expecting a string Key for the second array. It it's supposed to be an array of arrays, you should be using an array and not an Object.

Related

JavaScript - Modify localstorage json

I have the following Json, in the browser's localstorage, I want to know if there is a way to take it and modify it with JavaScript.
The json you are seeing is modified to make it easier to understand the information, but it grows more than 22 thousand lines and exponentially due to the values ​​of the variable called pricelist.
What I want to do is :
grab the json from localstorage.
make a method that goes through all the json and when it finds the variable "pricelist" delete the content that has "items_ids" and "items"
reload the json to the same key
ANNOTATIONS: -the variable lines can be empty.
-the variable pricelist can be false.
Thank you very much for your attention and your comments.
[
{
"id":"00027-008-0001",
"data":{
"lines":[
[
0,
0,
{
"product_id":23603,
"pricelist":{
"id":10,
"item_ids":[
27069,
26894
],
"items":[
{
"id":20044,
"product_tmpl_id":[
13142,
"[DNCM1LT] DESENGRASANTE NARANJA CITRUSMAX 1 LT"
]
}
]
},
"uom_id":[
1
]
}
],
[
0,
0,
{
"product_id":23666,
"pricelist":false,
"uom_id":[
1
]
}
]
],
"pos_session_id":27
}
},
{
"id":"00027-008-0002",
"data":{
"lines":[
[
0,
0,
{
"product_id":23655,
"pricelist":false,
"uom_id":[
1
]
}
]
],
"pos_session_id":27
}
},
{
"id":"00027-008-0003",
"data":{
"lines":[
],
"pos_session_id":27
}
}]
You can do it like the following:
let obj = JSON.parse(localStorage.get("item"));
obj.key = "value";
localStorage.set("item", JSON.stringify(obj));
Checkout the window.localStorage
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Using dictionary as D3 data source

I want to use this data (below) as a data source for a d3.js program:
{
"component": {
"name1": {
"graphname": {
"title": "foo",
"data": [
{"data": "DATE IN ISOFORMAT", "value": 5},
{"data": "DATE IN ISOFORMAT", "value": 10}
]
}
},
"name2": {
"graphname": {
"title": "foo",
"data": [
{"data": "DATE IN ISOFORMAT", "value": 5},
{"data": "DATE IN ISOFORMAT", "value": 10}
]
}
}
}
"component2": {...
}
D3 accepts only array data? How i can manipulate it to work?
I want to graph for all component any "graphname" aggregated by "name"
Any tips?
D3 does not just accept arrays as data sources, but for looping purposes, arrays are much more convenient than JavaScript objects ("dicts"). There is a very easy way to convert objects to arrays, though. If your object above were called d, then its corresponding array can be created with:
var dlist = d3.entries(d);
Now dlist will be something like:
[ { key: 'component',
value: { name1: ..., name2: ... } },
{ key: 'component2',
value: { name1: ..., name2: ... } } ]
The original dict has been remapped into an array of "records," each with key and value pairs. This "array of records" pattern is very common in D3 work, and JavaScript in general. If you need to loop over the sub-structures (e.g. the name1, name2, ... values, d3.entries can be applied at multiple levels of the original structure, as those dict-to-list transforms are required.
Since this answer is called out in the comments as wrong, here's a working example of using an object ("dict") as a data source for a D3 program: first in a simple loop, then secondarily using the idiomatic d3 .data(...).enter() pipeline.

Angular bind to filtered count

I have this array:
[
{
type: "hhh",
items: [
{
"name": "EGFR",
"type": "a",
"selected": true
}
]
},
{
type: "aaa",
items: [
{
"name": "mm",
"type": "b",
"selected": false
}
]
},
{
type: "ii",
items: [
{
"name": "pp",
"type": "bb",
"selected": true
}
]
}
]
I want to show a counter of the items with selected property "true".
I want it to be changed real time when change.
(Without watch and function)
Thnaks!
Here is the way:
var current_selected = {
get amount(){
var res = 0;
arr.forEach(function(item, i, arr) {
if (item.items[0].selected) res++;
})
return res;
}
}
Calling:
current_selected.amount
Fiddle
You can use JsonPath to get the count. Also using JsonPath has an added advantage of working on complex json structure. For the example you gave, you just need to include jsonpath js file and use the following in your script:
console.log(arr);
var filtered = jsonPath(arr, "$.[*].items[?(#.selected==true)]");
console.log(filtered);
console.log(filtered.length);
where arr is your json structure.
JsonPath can be got from :
https://code.google.com/archive/p/jsonpath/downloads
JsonPath help:
http://goessner.net/articles/JsonPath/
There might be updated version in other sources but that was the one I had worked on

Mongo: cross referencing values in other objects

I have a collection that looks like the ff:
{
"word":"approve",
"related" : [
{
"relationshipType" : "cross-reference",
"words" : [
"note"
]
},
{
"relationshipType" : "synonym",
"words" : [
"demonstrate",
"ratify",
]
}
],
},
{
"word": "note",
"related" : [
{
"relationshipType" : "synonym",
"words" : [
"butt",
"need",
],
},
{
"relationshipType" : "hypernym",
"words" : [
"air",
"tone",
]
},
{
"relationshipType" : "cross-reference",
"words" : [
"sign",
"letter",
"distinction",
"notice",
]
},
],
}
I want to group/categorize all the objects which have a word in another object, be it
the name (as the cross-reference field of 'approve', has note. searches for the word 'note'.
or
the word is in another object's related words. like having a synonym of 'ratify' under 'approve' then looking for other objects that have have 'ratify' in any field of their related words.
Then save these to a new collection called categories.
result should Be:
{
"category": 1,
"words":["approve","note"],
}
...and the value of the word field for all the linked objects in the words array.
Any way how to do this.. i'm thinking about some sort of recursion in checking links but i'm not sure how to implement. another potential problem is going back to the parent layer creating an infinite loop of sorts.
and is it possible through map reduce?
EDIT: clarity.

Trouble parsing multi-level JSON

I have a chunk of JSON which looks something like:
{
"map": [
[
"zimbraFeatureCalendarUpsellEnabled",
"FALSE"
],
[
"zimbraPrefCalendarDayHourStart",
"8"
],
[
"zimbraFeatureOptionsEnabled",
"TRUE"
],
[
"zimbraAttachmentsViewInHtmlOnly",
"FALSE"
]
]
}
(and so on; there's 200+ entries)
I need to be able to pick out individual key/value pairs from the JSON response, either with jQuery or plain old Javascript. I haven't been able to figure out how to address a specific key, though. Any ideas?
What you've described is a single level object, with a whole bunch of nested arrays so accessing will be
myObject.map[entryNumber][0 or 1] // 0 == key, 1 == value
You probably want something akin to this (unless you're working with existing API or some such):
{
"map": {
"zimbraFeatureCalendarUpsellEnabled": "FALSE",
"zimbraPrefCalendarDayHourStart": "8",
...
}
}
Instead of using arrays, you could use an object:
{
map : {
"zimbraFeatureCalendarUpsellEnabled" : "FALSE",
"zimbraPrefCalendarDayHourStart" : "8",
"zimbraFeatureOptionsEnabled" : "TRUE",
"zimbraAttachmentsViewInHtmlOnly" : "FALSE"
}
}
and then to access it:
myJSONObject.map.zimbraFeatureCalendarUpsellEnabled;

Categories