JSON Hierarchy : Hesitating on how to implement it - javascript

I'm actually building a website where I need to display a hierarchy and I've the constraint to build the latter with JSON.
So before implementing anything, two possibilities comes to my mind for the hierarchy and I can't figure out which one will be the most efficient for my project.
Objective
I have to display on a webpage, using HTML5 BoilerPlate, a whole hierarchy represented in the dico_tree field. I'll have to parse this one with JavaScript in order to get all the data it contains.
Constraints
Items can have one or no parent (represented by null).
Items can have multiple or no children.
I'll have to find the fastest way possible each unique item in order to have a quick display on my webpage.
Possibilities I've thought about
Possibility 1
{
"dico_name" : "Dictionary",
"version" : "1",
"dico_tree" : [
{"ID" : 1,"parent" : null,"children" : [2]},
{"ID" : 2, "parent" : 1, "children": [3,4]},
{"ID" : 3, "parent" : 2, "children": null},
{"ID" : 4, "parent" : 2, "children": null},
{"ID" : 5,"parent" : null,"children" : [6]},
{"ID" : 6, "parent" : 5, "children": [7]},
{"ID" : 7, "parent" : 6, "children": null}],
"custom_translations_list" : [
{"TRANSLATION_ID" : 1, "CUSTOM_TRANSLATION_ID" : 12}
}
Possibility 2
{
"dico_name" : "Dictionary",
"version" : "1",
"dico_tree" : [
{"ID" : 1,"parent" : null,"children" : [
{
"ID" : 2, "parent" : 1, "childen": [
{
"ID" : 3, "parent" : 2, "children": null
},
{
"ID" : 4, "parent" : 2, "children": null
}
]
}
]},
{"ID" : 5,"parent" : null,"children" : [
{
"ID" : 6, "parent" : 5, "childen": [
{
"ID" : 7, "parent" : 6, "children": null
}
]
}
]}
],
"custom_translations_list" : [
{"TRANSLATION_ID" : 1, "CUSTOM_TRANSLATION_ID" : 12},
]
}
I'm up to add any precision if you need some !
Thanks for your time and help in advance :).

Does disco_tree have to be an array? Making it an object would allow you to access any item in O(1). Also, if an item with no children has an empty array [] instead of null, you can save yourself some annoying null pointer exceptions.
"dico_tree" : {
1 : {"ID" : 1, "parent" : null, "children" : [2] },
2 : {"ID" : 2, "parent" : 1, "children": [3,4] },
3 : {"ID" : 3, "parent" : 2, "children": [] },
4 : {"ID" : 4, "parent" : 2, "children": [] },
5 : {"ID" : 5, "parent" : null, "children" : [6]},
6 : {"ID" : 6, "parent" : 5, "children": [7] },
7 : {"ID" : 7, "parent" : 6, "children": [] }
},

Related

C#/Javascript: How to convert JSON string contains parents-child array into multiple parents? [duplicate]

This question already has answers here:
Map and reduce array of objects with a children array down to array of children with parent's id
(3 answers)
Closed 1 year ago.
I'd like to split parents-child array into multiple parents with one child. The JSON string as below:
[{
"Father-Name" : "Jack",
"Mother-Name" : "Rose",
"Children": [{ "Name" : "Lmao" }, {"Name" : "Lol"}]
},
{
"Father-Name" : "Donald",
"Mother-Name" : "Melissa",
"Children": null
}]
and my desired output:
[{
"Father-Name" : "Jack",
"Mother-Name" : "Rose",
"Children": { "Name" : "Lmao" }
},
{
"Father-Name" : "Jack",
"Mother-Name" : "Rose",
"Children": {"Name" : "Lol"}
},
{
"Father-Name" : "Donald",
"Mother-Name" : "Melissa",
"Children": null
}]
I did some searching but there's no matching for this.
The answer is in this link solution. Thanks to #JLRishe
I only modified a null check.
var data = [{
"Father" : "Jack",
"Mother" : "Rose",
"Children": [{ "Name" : "Lmao" }, {"Name" : "Lol"}]
},
{
"Father" : "Donald",
"Mother" : "Melissa",
"Children": null
}]
var result = data.map(d => d.Children!== null ? d.Children.map(c => ({"Father": d.Father,"Mother": d.Mother, Children: {...c}})) : d).flat();
console.log(result);

How to return the variant values of each product if that product is a variant?

I have a database in MongoDB like this
{"productId" : 1,
"isVariant": 1,
"variantId" : 1,
"attributeSet" : [
{
"name" : "Capacity",
"value" : "500 GB",
"id" : 3
},
{
"name" : "Form Factor",
"value" : "5 inch",
"id" : 4
},
{
"id" : 5,
"name" : "Memory Components",
"value" : "3D NAND"
}
]
},
{"productId" : 2,
"isVariant": 1,
"variantId" : 1,
"attributeSet" : [
{
"name" : "Capacity",
"value" : "1 TB",
"id" : 3
},
{
"name" : "Form Factor",
"value" : "5 inch",
"id" : 4
},
{
"id" : 5,
"name" : "Memory Components",
"value" : "3D NAND"
}
]
},
{"productId" : 3,
"isVariant": 1,
"variantId" : 1,
"attributeSet" : [
{
"name" : "Capacity",
"value" : "500 GB",
"id" : 3
},
{
"name" : "Form Factor",
"value" : "2.5 inch",
"id" : 4
},
{
"id" : 5,
"name" : "Memory Components",
"value" : "3D NAND"
}
]
},
{"productId" : 4,
"isVariant": 1,
"variantId" : 1,
"attributeSet" : [
{
"name" : "Capacity",
"value" : "1 TB",
"id" : 3
},
{
"name" : "Form Factor",
"value" : "2.5 inch",
"id" : 4
},
{
"id" : 5,
"name" : "Memory Components",
"value" : "3D NAND"
}
]
}
Now I want to return data where 500 GB has been in productId 1 and 3
The response should be like this:
variantValues : [{
attributeValue : "500 GB",
data : [
{productId : 1},
{productId : 3}
]},
{
attributeValue : "1 TB",
data : [
{productId : 2},
{productId : 4}
]},
{
attributeValue : "2.5 inch",
data : [
{productId : 3},
{productId : 4}
]},
{
attributeValue : "5 inch",
data : [
{productId : 1},
{productId : 2}
]}]
I have the possible values that I store in another collection for variantPossible values. The values that i am storing are like this:
"VariantValues" : {
"3" : [
"500 GB",
"1 TB"
],
"4" : [
"2.5 inch",
"5 inch"
]
},
I want to return the variant values of each product if that product is a variant with the above format. can anyone help me with this.
You should be able to achieve this using $unwind and $group in your aggregation pipeline. This first flattens each attribute into a single document and on those you can group by the attribute value.
Finally, you can use $project to get the desired name for attributeValue:
db.collection.aggregate([
{
$unwind: "$attributeSet"
},
{
$group: {
_id: "$attributeSet.value",
data: {
"$addToSet": {
productId: "$productId"
}
}
}
},
{
"$project": {
_id: 0,
data: 1,
attributeValue: "$_id"
}
}
])
See this simplifed example on mongoplayground: https://mongoplayground.net/p/VASadZnDedc

Using Javascript aggregation in druid queries

I am trying to write javascript aggregator for my druid queries. i need to to calculate average of a metric "Base_SalesRank".
So far i have been able to this by writing:
{
"queryType": "groupBy",
"dataSource": "marketdata",
"granularity": "all",
"dimensions" : ["Item"],
"filter": { "type": "and", "fields" : [{"type": "selector", "dimension": "Item", "value": "MN10CESWW"}]},
"intervals": ["2018-06-28T00:00Z/2018-07-04T00:00Z"],
"aggregations" : [
{ "type" : "count", "name" : "rows" },
{ "type" : "doubleSum", "name" : "Base_SalesRank", "fieldName" : "Base_SalesRank" }
],
"postAggregations" : [{
"type": "javascript",
"name": "Target DOS Average",
"fieldNames": ["Base_SalesRank", "rows"],
"function": "function(Base_SalesRank, rows) {return Base_SalesRank/ rows;}"
}]
}
But I noticed that many values in Base_SalesRank is 0.
[ {
"timestamp" : "2018-06-28T05:06:03.000Z",
"result" : {
"pagingIdentifiers" : {
"marketdata_2018-06-28T00:00:00.000Z_2018-06-29T00:00:00.000Z_2018-07-06T08:11:02.499Z" : 3
},
"dimensions" : [ "Item" ],
"metrics" : [ "Base_SalesRank" ],
"events" : [ {
"segmentId" : "marketdata_2018-06-28T00:00:00.000Z_2018-06-29T00:00:00.000Z_2018-07-06T08:11:02.499Z",
"offset" : 0,
"event" : {
"timestamp" : "2018-06-28T07:10:02.000Z",
"Item" : "MN10CESWW",
"Base_SalesRank" : 0
}
},
{
"segmentId" : "marketdata_2018-06-28T00:00:00.000Z_2018-06-29T00:00:00.000Z_2018-07-06T08:11:02.499Z",
"offset" : 3,
"event" : {
"timestamp" : "2018-06-28T07:20:21.000Z",
"Item" : "MN10CESWW",
"Base_SalesRank" : 5558
}
} ]
}
} ]
So i am not getting true average. Now i need to weed out these 0 values and then calucate average. We can do this by using filters
{"type": "not", "field": {"type": "selector", "dimension": "Base_SalesRank", "value": "0"}}
But I have constraint that I have to perform this filter operation inside the javascript function only.
You can achieve the same with just adding a having query -
"having": {
"type": "greaterThan",
"aggregation": "Base_SalesRank",
"value": 0
}
If you want to do the same in javascript function than it can be done as below -
You should add a dimension (key/value) say "isValid" as "0" or "1" during pre-ingestion json data based on if Base_SalesRank is 0 than "isValid" will be 0 else 1.
Apply filter on this field in your query.
Use the rows in your post Aggregration.

insert in subdocument with mongoDB

I have the following document in the collection:
"_id" : "2",
"workspace" : [{
"name" : "1",
"widgets" : [ ]
},{
"name" : "2",
"widgets" : [ ]
},{
"name" : "3",
"widgets" : [ ]
},{
"name" : "4",
"widgets" : [ ]
}
]}
How can I insert {id: "1", blabla: "blabla"} in "widgets" for the "name" 3?
In comparison to a previous answer which just inserts everything into the root of the document, here is a correct way to do this with positional operator:
db.t.update({
"_id" : "2",
"workspace.name" : "3"
},{
$push: {
'workspace.$.widgets' : {
id: "2",
blabla: "blabla"
}
}
});

Multi options in select2

I am trying to use select2 to get remote JSON data and display it with mutli levels.
http://ivaynberg.github.io/select2/index.html
This is my response
{
"Company": [
{
"name": "athenahealth Inc"
},
{
"name": "Localiza Rent a Car"
},
{
"name": "M and B Switchgears"
}
],
"Functional": [
{
"name": "arranger"
},
{
"name": "ambassadors"
}
],
"Persons": [
{
"name": "Moustapha al"
},
{
"name": "Saleh al"
}
]
}
I want to show the result in Multi-Value format - http://ivaynberg.github.io/select2/index.html#multi
So far i am able to fetch data from server side , but then i have no idea how to enable the multi select option.
JSON in following format will work fine
Related issue - https://github.com/ivaynberg/select2/issues/58
{ "Data" : [ {
"id" :1 ,
"text" : "Subsection" ,
"children" : [{
"id" : 2,
"text" : "Paru"
},
{
"id" : 3,
"text" : "Vinu"
}]
},
{ "id" : 4 ,
"text" : "Family" ,
"children" : [{
"id" : 5,
"text" : "ChildVM"
},
{
"id" : 6,
"text" : "ChildPM"
}]
}
]
}

Categories

Resources