Parsing a 2D array in handlebars.js - html

Am having the following JSON data in my hand, I need to create a table, but the data is dynamic(both row header and column header & cell values).
JSON structure as follows,
{
"data": {
"vertical_header": [
"football",
"hockey"
],
"horizontal_header": [
"China",
"Brazil",
"Australia"
],
"researchDetails": [
{
"country": "brazil",
"game": "hockey",
"player": "Mr.r"
},
{
"country": "china",
"game": "hockey",
"player": "Mr.X"
},
{
"country": "china",
"game": "football",
"player": "Mr.Y"
},
{
"country": "china",
"game": "football",
"player": "Mr.Z"
},
{
"country": "brazil",
"game": "football",
"player": "Mr.M"
},
{
"country": "brazil",
"game": "football",
"player": "Mr.E"
}
]
}
}
The following is the table structure, I have to parse such JSON to construct a dynamic table like this using handlebars template. The headers are also dynamic, I have to create an array (2D/3D) so that I can construct the following,
<html>
</body>
<table border="1">
<tr><th/><th>hockey</th><th>football</th></tr>
<tr><td rowspan="2">china</td><td>Mr.X</td><td>Mr.Y</td></tr>
<tr><td></td><td>Mr.Z</td></tr>
<tr><td rowspan="2">brazil</td><td>Mr.r</td><td>Mr.M</td></tr>
<tr><td ></td><td>Mr.e</td></tr>
<tr><td>Australia</td><td></td><td/></tr>
</table>
</body>
</html

Related

I need to retrieve the index(number 1) information in mongodb compass

I just need to query all information of the doctor in the first index.
Here is my sample XML data
Here is my sample JSON data:
Both XML and JSON data are same. I just converted the XML to JSON format.
I have more documents like this.
I tried this query in this image
this query showing all information but I just need first doctors information.
I also tried in command prompt.Here is the query done in cmd
Here is my sample JSON code block: but I have more similar documents
{
"doctors": {
"doctor":[
{
"ssn": "257-79-xxxx",
"name": "Mavis Bxx",
"address": "xxxx Rusk Drive",
"country": "France",
"email": "",
"phone": "",
"patients": {
"patient": [
{
"gender": "Male",
"name": "Itch xxxx",
"address": "xx Cottonwood Avenue",
"revenue": "254",
"_country": "Spain",
"_id": "27"
},
{
"gender": "Male",
"name": "Damon xxxxx",
"address": "xx David Trail",
"revenue": "370",
"_country": "Germany",
"_id": "21"
}
]
},
"_id": "6"
},
{
"ssn": "179-45-xxxx",
"name": "Tobie Conxxxx",
"address": "x Comanche Center",
"country": "Spain",
"email": "tconringh#xxx.xxx",
"phone": "+86 998 262 xxxx",
"patients": {
"patient": {
"gender": "Male",
"name": "Vergil Tome",
"address": "x Melody Drive",
"revenue": "254",
"_country": "Germany",
"_id": "15"
}
},
"_id": "18"
},
{
"ssn": "777-59-xxxx",
"name": "Gertrud Macxxxx",
"address": "x Buell Drive",
"country": "USA",
"email": "gmaclaig2#xxx.com",
"phone": "+62 975 394 xxxx",
"patients": {
"patient": [
{
"gender": "Non-binary",
"name": "Dre Skxxxx",
"address": "x Becker Circle",
"revenue": "400",
"_country": "Germany",
"_id": "20"
},
{
"gender": "Female",
"name": "Arleyne Lestxxxx",
"address": "xx Farragut Court",
"revenue": "225",
"_country": "France",
"_id": "22"
}
]
},
"_id": "3"
}
You needs to change your collect data format. Your current format is single big size data. Not a quarriable format.
So
From current format
{
"doctors": {
"docker": [
{
...
"patients": {
"patient": [
{
...
},
{
...
}
]
},
},
{
...
}
]
}
}
To this format
[
{
...
"patients": [
{
...
},
{
...
}
]
},
{
...
}
]
Test JSON
[
{
"ssn": "2xx-7x-4xxx",
"name": "Mavis Bxxxed",
"address": "9xxx Rusk Drive",
"country": "France",
"email": "",
"phone": "",
"patients": [
{
"gender": "Male",
"name": "Itch Txxx",
"address": "5xx Cottonwood Avenue",
"revenue": "25x",
"_country": "Spain",
"_id": "27"
},
{
"gender": "Male",
"name": "Damon Wxxx",
"address": "09xx Dxxxx Trail",
"revenue": "370",
"_country": "Germany",
"_id": "21"
}
],
"_id": "6"
},
{
"ssn": "19-45-xxxx",
"name": "Tobie Coxxxx",
"address": "8x Comxxxx Center",
"country": "Spain",
"email": "tconxxxxx#usa.gov",
"phone": "",
"patients": [
{
"gender": "Male",
"name": "Some one",
"address": "1 Mexxxx Drive",
"revenue": "254",
"_country": "Germany",
"_id": "15"
}
],
"_id": "18"
}
]
In Compass View after add data upper data
Find doctor by name query
{ name: "Mavis Bxxxed" }
Find doctor by patient name
{ "patients.name": "Some one" }
If same search with this query
{ "patients.name": "Itch Txxx" }
OR
{ "patients.name": "Damon Wxxx" }
will same result to find the first docker (Mavis Bxxxed)
Due to it's patients start [ and end ]. it is array type a single size of data.
You can filter by project option
{
patients: {
$filter:
{
input: "$patients",
cond: { $eq: [ "$$patient.name", "Itch Txxx"] },
as: "patient"
}
}
}
OR shows only address
{ "patients.address" : 1 }

Reply head informations and add extract array to same result

someone colud please help me with this situation?
I have this fake JSON...
[
{
"user": {
"type": "PF",
"code": 12345,
"Name": "Darth Vader",
"currency": "BRL",
"status": "SINGLE",
"localization": "NABOO",
"createDate": 1627990848665,
"olderAdress": [
{
"localization": "DEATH STAR",
"createDate": 1627990848775
},
{
"localization": "TATOOINE",
"createDate": 1627990555888
},
]
}
}
]
My idea is, i need to extract the "olderAdress" and create new register but I need to keep the original register too.
Example: This is the result I hope.
[
{
"_id": ObjectId("5a3456e000102030405000000"),
"user": {
"Name": "Darth Vader",
"code": 12345,
"createDate":1627990848665,
"currency": "BRL",
"localization": "NABOO",
"status": "SINGLE",
"type": "PF"
}
},
{
"_id": ObjectId("5a789e000102030405000000"),
"user": {
"Name": "Darth Vader",
"code": 12345,
"createDate": 1627990848775,
"currency": "BRL",
"localization": "DEATH STAR",
"status": "SINGLE",
"type": "PF"
}
},
{
"_id": ObjectId("5a991e000102030405000000"),
"user": {
"Name": "Darth Vader",
"code": 12345,
"createDate": 1627990555888,
"currency": "BRL",
"localization": "TATOOINE",
"status": "SINGLE",
"type": "PF"
}
}
]
I try same think in this link (Test to extract values) to tests but unfortunately I cant. Somewoone could please help me?
You're very close, what you want to do is just to add the new location to the array before $unwinding it.
like so:
db.collection.aggregate([
{
"$addFields": {
"user.olderAdress": {
"$concatArrays": [
"$user.olderAdress",
[
{
"localization": "NABOO",
"createDate": "$$NOW"
}
]
]
}
}
},
... rest of pipeline ...
])
Mongo Playground

Dynamically populating the number of tables based on JSON input obtained - angularJS

I'm trying to populate number of tables dynamically by the input I get via JSON in angularJS.
I am perplexed on what to use and how to use since I am a learner in angular.
Would be helpful if anybody could help.
I have this JSON obtained in my script.
{
"took": 167,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 22.126987,
"hits": [
{
"_type": "data",
"_id": "3423",
"_score": 22.126987,
"_source": {
"mdmId": "45234",
"fullName": "rick j cruz",
"firstName": "rick",
"middleName": "j",
"lastName": "cruz",
"gender": "MALE",
"employeeInd": "N",
"phoneList": [],
"partyAdresses": [
{
"addressType": "home",
"address1": "xxx",
"address2": "yyy",
"city": "SAN JOSE",
"state": "CA",
"zipCode": "zzz"
},
{
"addressType": "mailing",
"address1": "xxx",
"address2": "yyy",
"city": "SAN JOSE",
"state": "CA",
"zipCode": "zzz"
}
],
"policyList": [
{
"agencyCode": "50000",
"agentid": "500",
"sourceSystem": "unit",
"policyNumber": "123",
"policyNumberRaw": "123",
"policyStatus": "CANCELLED",
"roleList": [
{
"roleType": "INSURED",
"roleStatus": "ACTIVE",
},
{
"roleType": "LISTED DRIVER",
"roleStatus": "ACTIVE",
}
]
}
]
}
},
{
"_type": "data",
"_id": "3423",
"_score": 22.126987,
"_source": {
"mdmId": "45234",
"fullName": "rick j cruz",
"firstName": "rick",
"middleName": "j",
"lastName": "cruz",
"gender": "MALE",
"employeeInd": "N",
"phoneList": [],
"partyAdresses": [
{
"addressType": "home",
"address1": "xxx",
"address2": "yyy",
"city": "SAN JOSE",
"state": "CA",
"zipCode": "zzz"
},
{
"addressType": "mailing",
"address1": "xxx",
"address2": "yyy",
"city": "SAN JOSE",
"state": "CA",
"zipCode": "zzz"
}
],
"policyList": [
{
"agencyCode": "50000",
"agentid": "500",
"sourceSystem": "unit",
"policyNumber": "123",
"policyNumberRaw": "123",
"policyStatus": "CANCELLED",
"roleList": [
{
"roleType": "INSURED",
"roleStatus": "ACTIVE",
},
{
"roleType": "LISTED DRIVER",
"roleStatus": "ACTIVE",
}
]
}
]
}
}
]
}
}
I now need to populate two different tables based on the total value, mentioned in the JSON.
In case if the JSON returns 3, 3 tables needs to be populated. Eg: Below format.
Can someone help me with a fiddle or something?
Since you want to render out n "hits" with (I guess) som intensive styling and a lot more info than described, I think you should place it in a small directive :
angular.module('myApp').directive('hitTable', function() {
return {
scope: {
hit: '='
},
templateUrl: 'views/hitTable.html'
}
});
hitTable.html could look like this (based on sample boxes in the question, add more of the info you want rendered and you must do the styling yourself) :
<div>
<div> {{ hit._source.mdmId }}</div> <!-- sample box #1 -->
<div> {{ hit._source.firstName }}, {{ hit._source.lastName }}</div> <!-- sample box #2 -->
<div>
<span ng-repeat="address in hit._source.partyAdresses"> <!-- n of sample box #3 -->
{{ address.address1 }}
{{ address.city }}
{{ address.state }}
</span>
</div>
<div>
Once registered in your app and assuming $scope.data hold the sample JSON above you can do
<div ng-repeat="hit in data.hits.hits">
<hit-table hit="hit"></hit-table>
</div>
In my opinion this is the real power of AngularJS (and its like). With very little effort you can have a rather complex page structure. What you need now is to style each "hit", i.e the content of hitTable.html.
NB: I intentionally forget all about "total" here. The important is the content you want to render out, i.e whats within the JSON.

Rename fields in nested arrays using JOLT transformation

I want to rename fields in an array nested in an another array using JOLT transformation library.
1. One field to rename is a top level field in an array
2. Two fields to rename are inside a nested array
I have tried using wildcards but they are not giving me expected output. I am using JOLT 0.0.22 version.
Input JSON:
{
"country": "usa",
"state": [
{
"stateName": "TX",
"location": "south",
"cities": [
{
"name": "Austin",
"pop": "1M"
},
{
"name": "Dallas",
"pop": "2M"
}
]
},
{
"stateName": "CA",
"location": "west",
"cities": [
{
"name": "SanFran",
"pop": "3M"
},
{
"name": "LosAngeles",
"pop": "4M"
}
]
}
]
}
Expected Output :
{
"country": "usa",
"state": [
{
"stateName": "TX",
"locatedIn": "south", // name change here
"cities": [
{
"cityname": "Austin", // name change here
"citypopulation": "1M" // name change here
},
{
"cityname": "Dallas",
"citypopulation": "2M"
}
]
},
{
"stateName": "CA",
"locatedIn": "west",
"cities": [
{
"cityname": "SanFran",
"pop": "3M"
},
{
"cityname": "LosAngeles",
"citypopulation": "4M"
}
]
}
]
}
Spec
[
{
"operation": "shift",
"spec": {
"country": "country",
"state": {
"*": { // state array index
"stateName": "state[&1].stateName",
"location": "state[&1].location",
"cities": {
"*": { // city array index
"name": "state[&3].cities[&1].cityname",
"pop": "state[&3].cities[&1].citypopualtion"
}
}
}
}
}
}
]
For comparison, this is the solution in JSLT.
{
"state" : [for (.state) . | {
"locatedIn" : .location,
"cities" : [for (.cities) {
"cityname" : .name,
"citypopulation" : .pop
}],
* : .
}],
* : .
}

Data structure for a family tree with multiple partners and siblings?

I have a very basic family tree structure but I need to figure out how to make it support multiple partners and siblings without as much redundancy.
The base of the entire tree is the person that's creating the tree.
Consider this very simple structure:
{
"name": "Me",
"dob": "1988",
"parents": [
{
"name": "Gina Carano",
"dob": "1967"
},
{
"name": "Genghis Khan",
"dob": "1961"
}
],
"children": [
{
"name": "Tim",
"dob": "1992"
}
]
}
This works nicely but what if I discovered I had a half sibling named Judy (Genghis Khan loved the ladies) and a full sibling named Brian and expanded it to this?
{
"name": "Me",
"dob": "1988",
"parents": [
{
"name": "Gina Carano",
"dob": "1967"
},
{
"name": "Genghis Khan",
"dob": "1961"
}
],
"children": [
{
"name": "Tim",
"dob": "1992"
}
],
"siblings": [
{
"name": "Judy",
"dob": "1987",
"parents": [
{
"name": "Courtney Carano",
"dob": "1965"
},
{
"name": "Genghis Khan",
"dob": "1961"
}
]
},
{
"name": "Brian",
"dob": "1988",
"parents": [
{
"name": "Gina Carano",
"dob": "1967"
},
{
"name": "Genghis Khan",
"dob": "1961"
}
]
}
]
}
This does map my 2 newfound siblings but now I have a bit of redundancy in my data, as Genghis Khan is in 3 different places. I could potentially create a one level list such as this:
[
{ "id": "1", "name": "Me", "dob": "1988", "parents": [2,3], "siblings": [4,5] },
{ "id": "2", "name": "Genghis Khan", "dob": "1961", "children": [1,4,5] },
{ "id": "3", "name": "Gina Carano", "dob": "1967", "children": [1] },
{ "id": "4", "name": "Tim", "dob": "1992", "parents" : [2,3] },
{ "id": "5", "name": "Judy", "dob": "1987", "parents": [2,6] },
{ "id": "6", "name": "Courtney Carano", "dob": "1965", "children": [5] }
]
Would this work out the same way without as much redundancy? And are there any foreseeable circumstances in which there would be any limitations in terms of mapping out multiple partners with children?
Note: I figure if I keep the initial structure, I'd have to add id keys to properly identify that Genghis Khan is the same in all 3 instances.
My end goal is mapping a pedigree tree (probably in d3.js) that is visually going to be in this manner, with a line in the middle between partners going to their children.
So with the dataset above, I'm trying to render:
Almost all genealogy systems have IDs for people, so I wouldn't worry about adding/requiring that.
The traditional way of doing this is to have a Family node type as well as a Person node type. This allows multiple marriages and also gives you a place to connect information like marriage date, marriage place, etc.
person[
{ "id": "p1", "name": "Me", "dob": "1988", "parents": "f3" },
{ "id": "p2", "name": "Genghis Khan", "dob": "1961", "parents": "f1", "spouse_families": ["f2", "f3"] },
{ "id": "p3", "name": "Gina Carano", "dob": "1967", "spouse_families" : ["f3"] },
{ "id": "p4", "name": "Brian", "dob": "1992", "parents" : "f3" },
{ "id": "p5", "name": "Judy", "dob": "1987", "parents": "f2" },
{ "id": "p6", "name": "Courtney Carano", "dob": "1965", "spouse_families": ["f2"] },
{"id": "p7", "name": "Mother of Ghengis"},
{"id": "p8", "name": "Father of Ghengis"},
]
family[
{"id":"f1","marriage date":"", "parents": ["p7", "p8"],"children":["p2"]},
{"id":"f2","marriage date":"", "parents": ["p6", "p2"],"children":["p5"]},
{"id":"f3","marriage date":"", "parents": ["p3","p2"],"children":["p1", "p4"]},
]
This gives you a place to connect all the parents and children together without redundancy and lots of special casing. (Note: I corrected "Tim" to "Brian" in the data structure to match the graphic.)