i would like to ask how i can achieve a result what i want i tried in different ways but i couldn't do, this is the json
[
[
{
"id": "2059623",
"name": "England-Belgium",
"tournament_stageFK": "841049",
"startdate": "2018/06/28 18:00:00",
"status_type": "Not started",
"status_descFK": "1",
"n": "4",
"ut": "2018/06/27 12:31:39",
"tournamentFK": "9731",
"tournament_templateFK": "77",
"sportFK": "1",
"tournament_stage_name": "World Cup Grp. G",
"tournament_name": "2018",
"tournament_template_name": "World Cup",
"sport_name": "Football",
},
{
"id": "2059624",
"name": "Panama-Tunisia",
"tournament_stageFK": "841049",
"startdate": "2018/06/28 18:00:00",
"status_type": "Not started",
"status_descFK": "1",
"n": "7",
"ut": "2018/06/27 12:34:01",
"tournamentFK": "9731",
"tournament_templateFK": "77",
"sportFK": "1",
"tournament_stage_name": "World Cup Grp. G",
"tournament_name": "2018",
"tournament_template_name": "World Cup",
"sport_name": "Football",
},
],
[
{
"id": "2681772",
"name": "Piteaa IF-Vittsjoe GIK",
"tournament_stageFK": "853166",
"startdate": "2018/06/28 17:00:00",
"status_type": "Not started",
"status_descFK": "1",
"n": "1",
"ut": "2017/12/18 15:02:33",
"tournamentFK": "12327",
"tournament_templateFK": "9089",
"sportFK": "1",
"tournament_stage_name": "Damallsvenskan",
"tournament_name": "2018",
"tournament_template_name": "Damallsvenskan",
"sport_name": "Football",
}
],
[
{
"id": "2705231",
"name": "Ifoe Bromoella IF-IFK Haessleholm",
"tournament_stageFK": "853950",
"startdate": "2018/06/28 17:00:00",
"status_type": "Not started",
"status_descFK": "1",
"n": "1",
"ut": "2018/02/12 13:02:19",
"tournamentFK": "12458",
"tournament_templateFK": "170",
"sportFK": "1",
"tournament_stage_name": "2. Division Ostra Gotaland",
"tournament_name": "2018",
"tournament_template_name": "2. Division",
"sport_name": "Football",
}
]
]
if you look closer you will notice that games are grouped by tournamentFK this is good now what i want is to clear a little bit this JSON the result that i want it is like this:
{
"competitionId": "456789",
"competitionName": "World Cup 2018",
"matches":[
{
"matchId":1234,
"teamA":"England",
"teamB":"Poland",
"time":"05:00",
"result":"3-3"
"stadium":"somewhere",
"halfTimeScore":"1-2"
},
{
"matchId":9876,
"teamA":"Spain",
"teamB":"Morocco",
"time":"05:00",
"result":"3-3"
"stadium":"somewhere",
"halfTimeScore":"2-1"
}
]
}
i need to get only one time tournamentFK and tournament_template_name and then to loop inside for each tournament and to make one clearly object.
my code:
fixturesTofilter.forEach((fixture) => {
sameGroup.push(fixture.tournamentFK);
competitionName.push(fixture.tournament_template_name);
});
const groupByTournament = sameGroup.filter((item, pos) => sameGroup.indexOf(item) === pos);
const clearSameCompetition = competitionName.filter((item, pos) => competitionName.indexOf(item) === pos);
// console.log(clearSameCompetition);
groupByTournament.forEach((byTournamentFk, index) => {
const grouped = groupBy(fixturesTofilter, item => item.tournamentFK);
groupedFixtures.push(grouped.get(byTournamentFk));
});
console.log(groupedFixtures);
You can easily flatten this object via lodash. Since the JSON object is flatterned, you can just add the tournamentFK and tournament_template_name. Hope this is what you are looking for.
const _ = require('lodash');
const obj = [
[
{
"id": "2059623",
"name": "England-Belgium",
"tournament_stageFK": "841049",
"startdate": "2018/06/28 18:00:00",
"status_type": "Not started",
"status_descFK": "1",
"n": "4",
"ut": "2018/06/27 12:31:39",
"tournamentFK": "9731",
"tournament_templateFK": "77",
"sportFK": "1",
"tournament_stage_name": "World Cup Grp. G",
"tournament_name": "2018",
"tournament_template_name": "World Cup",
"sport_name": "Football"
},
{
"id": "2059624",
"name": "Panama-Tunisia",
"tournament_stageFK": "841049",
"startdate": "2018/06/28 18:00:00",
"status_type": "Not started",
"status_descFK": "1",
"n": "7",
"ut": "2018/06/27 12:34:01",
"tournamentFK": "9731",
"tournament_templateFK": "77",
"sportFK": "1",
"tournament_stage_name": "World Cup Grp. G",
"tournament_name": "2018",
"tournament_template_name": "World Cup",
"sport_name": "Football"
}
],
[
{
"id": "2681772",
"name": "Piteaa IF-Vittsjoe GIK",
"tournament_stageFK": "853166",
"startdate": "2018/06/28 17:00:00",
"status_type": "Not started",
"status_descFK": "1",
"n": "1",
"ut": "2017/12/18 15:02:33",
"tournamentFK": "12327",
"tournament_templateFK": "9089",
"sportFK": "1",
"tournament_stage_name": "Damallsvenskan",
"tournament_name": "2018",
"tournament_template_name": "Damallsvenskan",
"sport_name": "Football"
}
],
[
{
"id": "2705231",
"name": "Ifoe Bromoella IF-IFK Haessleholm",
"tournament_stageFK": "853950",
"startdate": "2018/06/28 17:00:00",
"status_type": "Not started",
"status_descFK": "1",
"n": "1",
"ut": "2018/02/12 13:02:19",
"tournamentFK": "12458",
"tournament_templateFK": "170",
"sportFK": "1",
"tournament_stage_name": "2. Division Ostra Gotaland",
"tournament_name": "2018",
"tournament_template_name": "2. Division",
"sport_name": "Football"
}
]
]
const formatted = _.flattenDeep(obj)
const res = formatted.map(item => {
const newObj = {
matchId: item.tournamentFK,
teamA: item.name.split("-")[0],
teamB: item.name.split("-")[1],
time: item.startdate,
result:"how ??",
stadium:"somewhere",
halfTimeScore:"How?"
}
return newObj
})
console.log(res)
Related
I'm getting data in an specific way from an API and I have to convert it to a cleaner version of it.
What I get from the API is a JSON like this (you can see that there is some information duplicated as for the first fields but the investor is different).
{
"clubhouse": [
{
"id": "01",
"statusId": "ok",
"stateid": "2",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1234",
"gender": "01"
},
"inamount": "1500000",
"ratio": "12"
}
]
},
{
"id": "01",
"statusId": "ok",
"stateid": "2",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "4321",
"gender": "02"
},
"inamount": "1700000",
"ratio": "12"
}
]
},
{
"id": "02",
"statusId": "ok",
"stateid": "2",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1333",
"gender": "01"
},
"inamount": "1500000",
"ratio": "12"
}
]
},
{
"id": "03",
"statusId": "ok",
"stateid": "5",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "",
"gender": ""
},
"inamount": "",
"ratio": ""
}
]
},
{
"id": "02",
"statusId": "ok",
"stateid": "2",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1334",
"gender": "02"
},
"inamount": "1900000",
"ratio": "12"
}
]
}
]
}
I need to merge the investors and eliminate the duplicated information, the the expected result will be
{
"clubhouse": [
{
"id": "01",
"statusId": "ok",
"stateid": "2",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1234",
"gender": "01"
},
"inamount": "1500000",
"ratio": "12"
},
{
"investor": {
"id": "4321",
"gender": "02"
},
"inamount": "1700000",
"ratio": "12"
}
]
},
{
"id": "02",
"statusId": "ok",
"stateid": "2",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1333",
"gender": "01"
},
"inamount": "1500000",
"ratio": "12"
},
{
"investor": {
"id": "1334",
"gender": "02"
},
"inamount": "1900000",
"ratio": "12"
}
]
},
{
"id": "03",
"statusId": "ok",
"stateid": "5",
"TypeId": "3",
"investors": [
{
"investor": {
"id": "1555",
"gender": "01"
},
"inamount": "2000000",
"ratio": "15"
}
]
}
]
}
I'd try a couple of JOLTS and I got to merge the fields but not eliminate the duplicates.
You can start with grouping by id values such as
[
{
// group by "id" values to create separate objects
"operation": "shift",
"spec": {
"*": {
"*": {
"*": "#(1,id).&",
"investors": {
"*": {
"*": {
"#": "#(4,id).&3[&4].&" // &3 -> going 3 levels up to grab literal "investors", [&4] -> going 4 levels up the tree in order to reach the indexes of "clubhouse" array, & -> replicate the leaf node values for the current key-value pair
}
}
}
}
}
}
},
{
// get rid of "null" values
"operation": "modify-overwrite-beta",
"spec": {
"*": "=recursivelySquashNulls"
}
},
{
// pick only the first components from the repeated values populated within the arrays
"operation": "cardinality",
"spec": {
"*": {
"*": "ONE",
"investors": "MANY"
}
}
},
{
// get rid of object labels
"operation": "shift",
"spec": {
"*": ""
}
}
]
I could have sworn I've seen examples of this in multiple places, but can anyone tell me how I only return items from the JSON object below that match ALL of the tags in a search array - eg:
[
{
"ref": "1",
"title": "Title 1",
"description": "Description 1...",
"tags": ["A2", "B1", "D1", "E3", "E4"]
},
{
"ref": "2",
"brand": "Title 2",
"description": "Description 2...",
"tags": ["A1", "A2", "C1", "D1", "E1", "E2", "E3", "E4"]
},
{
"ref": "3",
"title": "Title 3",
"description": "Description 3...",
"tags": ["A5", "A6", "B1", "D1", "E3"]
},
{
"ref": "4",
"title": "Title 4",
"description": "Description 4...",
"tags": ["A5", "A6", "B1", "E1", "E3"]
}
]
And my search array to match/find within the tags array would be:
["B1", "D1"]
This would return the filtered JSON with only item ref 1 and 2 in this instance...
If you are using java script then this should work..
const array1 = [
{
"ref": "1",
"title": "Title 1",
"description": "Description 1...",
"tags": ["A2", "B1", "D1", "E3", "E4"]
},
{
"ref": "2",
"brand": "Title 2",
"description": "Description 2...",
"tags": ["A1", "A2", "C1", "D1", "E1", "E2", "E3", "E4"]
},
{
"ref": "3",
"title": "Title 3",
"description": "Description 3...",
"tags": ["A5", "A6", "B1", "D1", "E3"]
},
{
"ref": "4",
"title": "Title 4",
"description": "Description 4...",
"tags": ["A5", "A6", "B1", "E1", "E3"]
}
]
let tags = [ "B1", "D1"]
console.log(array1.filter( f => tags.every(e => f.tags.includes(e)))
);
I am looking to refine my data output by the category names.
How to filter the output items by the cat_array? Whilst preserving my text search filter?
{
"id": 1,
"title": "Title one",
"category_data": {
"2": "Team",
"7": "Queries"
}
},
I had tested using this:
return vm.info.filter(item => this.cat_array.includes(item.category_id))
Codepen example: https://codepen.io/anon/pen/XxNORW?editors=1011
Thanks
One approach - (Didn't take care of the All logic):
this.info.filter(
item => Object.values(item.category_data).some(
cat => this.cat_array.includes(cat))
)
Demo:
var info = [
{
"id": 1,
"title": "Title one",
"category_data": {
"2": "Team",
"7": "Queries"
}
},
{
"id": 2,
"title": "Title two",
"category_data": {
"2": "Team",
"7": "Queries"
}
},
{
"id": 3,
"title": "Title three",
"category_data": {
"2": "Team",
"7": "Queries"
}
},
{
"id": 4,
"title": "Title four",
"category_data": {
"2": "Team",
"7": "Queries"
}
},
{
"id": 5,
"title": "Title five",
"category_data": {
"2": "Team",
"6": "Questions",
"7": "Queries"
}
},
{
"id": 6,
"title": "Title six",
"category_data": {
"2": "Team",
"6": "Questions",
"7": "Queries",
"12": "Fax",
}
}
]
var cat_array = ["Questions"]
console.log(
info.filter(
item => Object.values(item.category_data).some(
cat => cat_array.includes(cat)
)
)
)
If I have records for users and also records for orders, it is proper to save them like that:
{
"users": [
{"id": "1", "name": "Allan", "age": "40"},
{"id": "2", "name": "Jack", "age": "50"}
],
"orders": [
{ "id": "1", "item": "item 1", "userId": "1"},
{ "id": "2", "item": "item 3", "userId": "1"},
{ "id": "1", "item": "item 4", "userId": "1"},
{ "id": "1", "item": "item 51", "userId": "1"},
{ "id": "2", "item": "item 4", "userId": "1"}
]
}
as you can see I'm coming from the SQL world so I'm saving the 'orders' data with foreign key, is that the right way to save this data as json and if so how can I get all the users with there orders?
Thanks!
If your json object is in the above mentioned structure you can use this code to filter the orders
var oJson = {
"users": [
{"id": "1", "name": "Allan", "age": "40"},
{"id": "2", "name": "Jack", "age": "50"}
],
"orders": [
{ "id": "1", "item": "item 1", "userId": "1"},
{ "id": "2", "item": "item 3", "userId": "1"},
{ "id": "1", "item": "item 4", "userId": "1"},
{ "id": "1", "i`enter code here`tem": "item 51", "userId": "1"},
{ "id": "2", "item": "item 4", "userId": "1"}
]}
for(var i=0; i<oJson.users.length;i++){
for(var j=0; j<oJson.orders.length;j++){
if (oJson.users[i].id == oJson.orders[j].userId) {
console.log( oJson.orders[j].item, oJson.orders[j].userId)
//your logic here
}
}
}
But it is better to use sql joints to merge the orders and return the objects like this
{
"users": [
{"id": "1", "name": "Allan", "age": "40","orders": [{ "id": "1", "item": "item 1", "userId": "1"},{ "id": "2","item": "item 3", "userId": "1"},{ "id": "1", "item": "item 4", "userId": "1"},{ "id": "1", "item": "item 51", "userId": "1"},{ "id": "2", "item": "item 4", "userId": "1"}
]},
{"id": "2", "name": "Jack", "age": "50","orders":[]}
]}
Fetch the objects for json file into two varible
var a=value.users;
var b=value.orders;
value is nothing but the whole json file
then you can match them against each other like
a[0].id==b.[0].id
I am trying to implement partial matching using ngrams in elasticsearch but not getting the expected results out of it.
I am following this link:-
https://www.elastic.co/guide/en/elasticsearch/guide/current/_index_time_search_as_you_type.html
I have done all the things which are mentioned in this link. My dataset contains 3 fields i.e id,name,age.
Here is my mapping and setting of my_index
GET /my_index/_settings
{
"my_index": {
"settings": {
"index": {
"creation_date": "1433249154544",
"uuid": "hKxHVnqaRVmji31xK92pVA",
"number_of_replicas": "1",
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": "1",
"max_gram": "20"
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"filter": [
"lowercase",
"autocomplete_filter"
],
"tokenizer": "standard"
}
}
},
"number_of_shards": "1",
"version": {
"created": "1040499"
}
}
}
}
}
GET /my_index/_mapping/my_type
{
"my_index": {
"mappings": {
"my_type": {
"properties": {
"#timestamp": {
"type": "date",
"format": "dateOptionalTime"
},
"#version": {
"type": "string"
},
"age": {
"type": "long"
},
"host": {
"type": "string"
},
"id": {
"type": "string"
},
"message": {
"type": "string"
},
"name": {
"type": "string",
"index_analyzer": "autocomplete",
"search_analyzer": "standard"
},
"path": {
"type": "string"
},
"type": {
"type": "string"
}
}
}
}
}
}
My query to elastic search is :-
GET /my_index/my_type/_search
{
"query": {
"match": {
"name": {
"query": "raman r"
}
}
}
}
According to me now the results should display only "raman ram" but it is also showing other results as well:-
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 9,
"max_score": 2.6631343,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "2",
"_score": 2.6631343,
"_source": {
"message": [
"2,raman,23"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.041Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "2",
"name": "raman",
"age": 23
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "10",
"_score": 1.8003473,
"_source": {
"message": [
"10,raman ram,43"
],
"#version": "1",
"#timestamp": "2015-06-02T13:11:03.455Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "10",
"name": "raman ram",
"age": 43
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "1",
"_score": 0.26245093,
"_source": {
"message": [
"1,Ram,342"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.040Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "1",
"name": "Ram",
"age": 342
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "3",
"_score": 0.26245093,
"_source": {
"message": [
"3,ramayan,23"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.041Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "3",
"name": "ramayan",
"age": 23
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "4",
"_score": 0.26245093,
"_source": {
"message": [
"4,ramaram,231"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.041Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "4",
"name": "ramaram",
"age": 231
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "5",
"_score": 0.26245093,
"_source": {
"message": [
"5,rampy,1"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.041Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "5",
"name": "rampy",
"age": 1
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "6",
"_score": 0.26245093,
"_source": {
"message": [
"6,ration,11"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.041Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "6",
"name": "ration",
"age": 11
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "7",
"_score": 0.26245093,
"_source": {
"message": [
"7,rita,42"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.042Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "7",
"name": "rita",
"age": 42
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "8",
"_score": 0.26245093,
"_source": {
"message": [
"8,roni,45"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.050Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "8",
"name": "roni",
"age": 45
}
}
]
}
}
I get correct results when i use "minimum_should_match": "100%" while querying.
GET /my_index/my_type/_search
{
"query": {
"match": {
"name": {
"query": "raman r",
"minimum_should_match": "100%"
}
}
}
}
Gave me better result although ranking is not right:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 2.6631343,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "2",
"_score": 2.6631343,
"_source": {
"message": [
"2,raman,23"
],
"#version": "1",
"#timestamp": "2015-06-02T13:07:18.041Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "2",
"name": "raman",
"age": 23
}
},
{
"_index": "my_index",
"_type": "my_type",
"_id": "10",
"_score": 1.8003473,
"_source": {
"message": [
"10,raman ram,43"
],
"#version": "1",
"#timestamp": "2015-06-02T13:11:03.455Z",
"type": "my_type",
"host": "shubham-VirtualBox",
"path": "/home/shubham/sample.csv",
"id": "10",
"name": "raman ram",
"age": 43
}
}
]
}
}
Don't know whether this approach is correct or not but do tell me if any alternative is there for this