React table with JSON subdocument data only - reactjs

I have nested arrays in JSON object that comes from CosmosDB. I have 3 subdocuments in the JSON which I want to display that data into a react table for each subdocument. How can we map each subdocument to a React Table Rows?
Below is the screenshot that I am expecting.
below is JSON data
[
{
"id": "DD3",
"studydate": "DDD",
"studydescription": "DD3 Description",
"studyname": "DD3",
"table1": [
{
"no": "1",
"name": "DD3 Name",
"date": "Krishna",
"description\r": "1111\r"
},
{
"no": "2",
"name": "DD3 Nam2",
"date": "Test2",
"description\r": "2222\r"
},
{
"no": "3",
"name": "DD3 Name3",
"date": "Test3",
"description\r": "3333"
}
],
"table2": [
{
"No": "2",
"Study Field1": "21",
"Study Field2": "22",
"Study Field3\r": "23"
}
],
"table3": [
{
"No": "3",
"Study Field5": "T31",
"Study Field6": "T32",
"Study Field7": "T33",
"Study Field 8\r": "T34"
}
],
"_rid": "QeNcANZFTTIKAAAAAAAAAA==",
"_self": "dbs/QeNcAA==/colls/QeNcANZFTTI=/docs/QeNcANZFTTIKAAAAAAAAAA==/",
"_etag": "\"33002e92-0000-0200-0000-5fa6fe320000\"",
"_attachments": "attachments/",
"_ts": 1604779570
}
]

I have a basic example of how you could generate the rows off JSON. I am just using basic HTML to keep it simple as the prop names will change depending what component you go with but same principle.
JavaScript for filtering and pushing to table array.
let rowArr = [];
Object.keys(data).forEach(function(key) {
rowArr.push(data[key])
})
HTML/JS
<table>
<!-- This should loop through rows allowing you to get row values -->
{rowArr.map(row => (
<tr key={row}>
<td>{row.Task}</td>
<td>{row.Description}</td>
<td>{row.Assignee}</td>
<td>{row.Client}</td>
<td>{row.Contact}</td>
</tr>
))}
</table>
Here is a simple JSON structure
let data =
{
1:{
Task: "Software",
Description: "Mockup designs",
Assignee: "John Doe",
Client: "Mark Wong",
Contact: "mark.wong#gmail.com"
}
}

Related

How to filter JSON data based on another JSON data in typescript

I have 2 JSON Data 1. Payers 2. Rules. I need to filter Payers JSON data based on PayerId from Rules JSON data.
{
"Payers": [
{
"payerId": "12345",
"name": "Test Payer1"
},
{
"payerId": "23456",
"name": "Test Payer2",
},
{
"payerId": "34567",
"name": "Test Payer3"
}}
Rules JSON file
{
"Rules": [
{
"actions": {
"canCopyRule": true
},
"RuleId": 123,
"description": "Test Rule",
"isDisabled": false,
"Criteria": [
{
"autoSecondaryCriteriaId": 8888,
"criteriaType": { "code": "primaryPayer", "value": "Primary Payer" },
"payerId": ["12345", "34567"]
}
]
}
}]}
I need to filter Payers JSON data based on Rules JSON data if PayerID matches
I need output like below
{
"Payers": [
{
"payerId": "12345",
"name": "Test Payer1"
},
{
"payerId": "34567",
"name": "Test Payer3"
}
}
How to filter?
You can use Array.filter like that (based on your data structure):
const filteredPayers = payersObj.Payers.filter((p) => rulesObj.Rules[0].Criteria[0].payerId.includes(p.payerId));
I can't figure out why your Rules json looks like this, I guess you have multiple rules. If so, you will need to iterate over each rule and invoke includes. Same for Criteria.
Code will check each rule and each critirias
and will return payers if payerId found in any of the given rules of any criteria
const payers = {
"Payers": [
{
"payerId": "12345",
"name": "Test Payer1"
},
{
"payerId": "23456",
"name": "Test Payer2",
},
{
"payerId": "34567",
"name": "Test Payer3"
}]}
const rules = {
"Rules": [
{
"actions": {
"canCopyRule": true
},
"RuleId": 123,
"description": "Test Rule",
"isDisabled": false,
"Criteria": [
{
"autoSecondaryCriteriaId": 8888,
"criteriaType": { "code": "primaryPayer", "value": "Primary Payer" },
"payerId": ["12345", "34567"]
}
]
}
]
}
const data = payers.Payers.filter(payer => rules.Rules.findIndex(rule => rule.Criteria.findIndex(criteria => criteria.payerId.includes(payer.payerId)) != -1) !== -1)
console.log(data)

Solr - Search parent having multiple child documents

I have a Solr index with structure as below.
{
"id": "1",
"bookName_s": "Core Concepts",
"bookDescription_s": "This is the description",
"isbn_s": "ABCD:123",
"reviews": [
{
"id": "2",
"Name_s": "review1",
"detail_s": "sample review"
}
],
"students": [
{
"id": "3",
"Name_s": "student1",
"student_s": "test student"
}
]
}
How do i search for parent that has a reviewer with Name_s as 'review1' and student with Name_s as 'student'.
I tried parent block chain query like below but nothing seems to work -
q=({!parent which="*:* -_nest_path_:*"}(+_nest_path_:\/reviews +Name_s:*rev*)) AND ({!parent which="*:* -_nest_path_:*"}(+_nest_path_:\/students +Name_s:*stu*))
q=({!parent which="*:* -_nest_path_:*"}(+_nest_path_:\/reviews +Name_s:*rev*)(+_nest_path_:\/students +Name_s:*stu*))
Is there a way i can acheive this using the q operator instead of fq parameter? thanks
Based on EricLavault suggestion i modified the index to include type of the object in the index like below -
{
"id": "1",
"bookName_s": "Core Concepts",
"bookDescription_s": "This is the description",
"isbn_s": "ABCD:123",
"type_s":"book"
"reviews": [
{
"id": "2",
"Name_s": "review1",
"detail_s": "sample review",
"type":"review"
}
],
"students": [
{
"id": "3",
"Name_s": "student1",
"type":"student",
"student_s": "test student"
}
]
}
and below queries worked.
{!parent which="type:book"}(type:review AND Name_s:review1) OR (type:student AND Name_s:student1)
returns all books with review1 and student1

How to use OData filter in dynamic array

I am trying to filter nested array using ?$filter in odata filter
but it is not working properly
parent array got filtered but not child one.
My Array
{
"value": [
{
"Id": 1,
"Country": "India",
"language": [
{
"Lid": 1,
"State": "telengana",
"Statuelanguage": "Telgu",
"Place to visit": [
"p3","p4"
]
},
{
"Lid": 2,
"State": "Delhi",
"Statuelanguage": "Hindi",
"Place to visit": [
"p5","p6"
]
},
{
"Lid": 3,
"State": "UP",
"Statuelanguage": "Hindi",
"Place to visit": [
"p7","p8"
]
}
]
}
]
}
Expected Responce
{
"value": [
{
"Id": 1,
"Country": "India",
"language": [
{
"Lid": 1,
"State": "telengana",
"Statuelanguage": "Telgu",
"Place to visit": [
"p3","p4"
]
}
]
}
]
}
Filter query
?$filter=language/any(c: c/Lid eq 1)
but when i am trying to use the filter, it is filtering the parent one not the child
it returns all 3 child to me
So it works as expected :)
$filter parameter is used to filter collection that you're querying.
To filter expanded/related collection (language in your case) you have to use expand filter feature:
...$expand=language($filter=Lid eq 1)
BUT: It is only possible in OData v4.
ref for webapi
nested filter description

How can i access data from a json nested structure having similar name using #Angular

I am learning angular and i came across a problem i want to get data from a json but the problem is the nested data from json data has similar name under torrent that is "URL" now i want to access two url by clicking two separate buttons is it possible.
{
"status": "ok",
"status_message": "Query was successful",
"data": {
"movie_count": 6025,
"limit": 20,
"page_number": 1,
"movies": [
{
"id": 6368,
"imdb_code": "tt2364897",
"title": "The Disappointments Room",
"title_english": "The Disappointments Room",
"title_long": "The Disappointments Room (2016)",
"slug": "the-disappointments-room-2016",
"year": 2016,
"rating": 3.9,
"runtime": 85,
"genres": [
"Drama",
"Horror",
"Thriller"
],
"language": "English",
"mpa_rating": "R",
"background_image": "https://new.ps/assets/images/movies/the_disappointments_room_2016/background.jpg",
"background_image_original": "https://new.ps/assets/images/movies/the_disappointments_room_2016/background.jpg",
"small_cover_image": "https://new.ps/assets/images/movies/the_disappointments_room_2016/small-cover.jpg",
"state": "ok",
"torrents": [
{
"url": "https://new.ps/torrent/download/C9FED33A10E67EB46373CB8F5E6FA6FD6AFD91E8",
"hash": "C9FED33A10E67EB46373CB8F5E6FA6FD6AFD91E8",
"quality": "720p",
"seeds": 800,
"peers": 563,
"size": "678.14 MB",
"size_bytes": 711081329,
"date_uploaded": "2017-03-06 16:03:23",
"date_uploaded_unix": 1488834203
},
{
"url": "https://new.ps/torrent/download/285CA3A886E8DE6FCF42D293A2404A8AD8F0CAC4",
"hash": "285CA3A886E8DE6FCF42D293A2404A8AD8F0CAC4",
"quality": "1080p",
"seeds": 675,
"peers": 462,
"size": "1.4 GB",
"size_bytes": 1503238554,
"date_uploaded": "2017-03-06 17:36:49",
"date_uploaded_unix": 1488839809
}
],
"date_uploaded": "2017-03-06 16:03:23",
"date_uploaded_unix": 1488834203
}
]
}
}
Assuming your json is in object called yourJsonObject then You can loop through this data in JS like this :
yourJsonObject.data.movies.forEach(function(movie){
movie.torrents.forEach(function(torrent){
console.log("Torrent URL : " + torrent.url);
});
});
If you are trying to do it in view in use ng-repeat directive.
<div ng-repeat="movie in yourJsonObject.data.movies">
<div ng-repeat="torrent in movie.torrents">
Torrent URL : {{torrent.url}} <br />
</div>
</div>

How to filter embedded array in mongo document with morphia

Given my Profile data looks like below, I want to find the profile for combination of userName and productId
and only return the profile with the respective contract for this product.
{
"firstName": "John",
"lastName": "Doe",
"userName": "john.doe#gmail.com",
"language": "NL",
"timeZone": "Europe/Amsterdam",
"contracts": [
{
"contractId": "DEMO1-CONTRACT",
"productId": "ticket-api",
"startDate": ISODate('2016-06-29T09:06:42.391Z'),
"roles": [
{
"name": "Manager",
"permissions": [
{
"activity": "ticket",
"permission": "createTicket"
},
{
"activity": "ticket",
"permission": "updateTicket"
},
{
"activity": "ticket",
"permission": "closeTicket"
}
]
}
]
},
{
"contractId": "DEMO2-CONTRACT",
"productId": "comment-api",
"startDate": ISODate('2016-06-29T10:27:45.899Z'),
"roles": [
{
"name": "Manager",
"permissions": [
{
"activity": "comment",
"permission": "createComment"
},
{
"activity": "comment",
"permission": "updateComment"
},
{
"activity": "comment",
"permission": "deleteComment"
}
]
}
]
}
]
}
I managed to find the solution how to do this from the command line. But I don't seem to find a way how to accomplish this with Morphia (latest version).
db.Profile.aggregate([
{ $match: {"userName": "john.doe#gmail.com"}},
{ $project: {
contracts: {$filter: {
input: '$contracts',
as: 'contract',
cond: {$eq: ['$$contract.productId', "ticket-api"]}
}}
}}
])
This is what I have so far. Any help is most appreciated
Query<Profile> matchQuery = getDatastore().createQuery(Profile.class).field(Profile._userName).equal(userName);
getDatastore()
.createAggregation(Profile.class)
.match(matchQuery)
.project(Projection.expression(??))
Note... meanwhile I found another solution which does not use an aggregation pipeline.
public Optional<Profile> findByUserNameAndContractQuery(String userName, String productId) {
DBObject contractQuery = BasicDBObjectBuilder.start(Contract._productId, productId).get();
Query<Profile> query =
getDatastore()
.createQuery(Profile.class)
.field(Profile._userName).equal(userName)
.filter(Profile._contracts + " elem", contractQuery)
.retrievedFields(true, Profile._contracts + ".$");
return Optional.ofNullable(query.get());
}
I finally found the best way (under assumption I only want to return max. 1 element from array) to filter embedded array.
db.Profile.aggregate([
{ $match: {"userName": "john.doe#gmail.com"}},
{ $unwind: "$contracts"},
{ $match: {"contracts.productId": "comment-api"}}
])
To match according to your first design you could try the projection settings with morphia aggregation pipeline.
Query<Profile> matchQuery = getDatastore().createQuery(Profile.class).field(Profile._userName).equal(userName);
getDatastore()
.createAggregation(Profile.class)
.match(matchQuery)
.project(Projection.expression("$filter", new BasicDBObject()
.append("input", "$contracts")
.append("as", "contract")
.append("cond", new BasicDBObject()
.append("$eq", Arrays.asList('$$contract.productId', "ticket-api")));
Also see the example written by the morphia crew around line 88 at https://github.com/mongodb/morphia/blob/master/morphia/src/test/java/org/mongodb/morphia/aggregation/AggregationTest.java.

Resources