How to iterate and render my fetch result in react native JSX - react-native

{
"status": true,
"live_score_domestic": [
{
"nTournamentID": "1",
"cTournamentName": "sample tournament.",
"cTournamentType": "D",
"dStartDate": "2016-12-10",
"dEndDate": "2016-12-12",
"matches": [
{
"cVenueCode": "TTAB",
"cTableName": "Table 1",
"cEventType": "Junior Boys",
"cMatchNo": "5",
"cRound": "First Round",
"nScheduledDate": "2016-12-11",
"nScheduledTime": "11:45:00",
"teamname1": "MOTHER SCHOOL",
"teamname2": "HARI SHEWA SCHOOL",
"nVenueID": "1",
"nTableID": "1",
"nTeamID1": "3",
"nTeamID2": "4",
"nTournamentID": "1",
"nFixtureDetailsID": "15",
"nEventTypeID": "5",
"image": "http://example.com/tt.png"
},
{
"cVenueCode": "TTAB",
"cTableName": "Table 1",
"cEventType": "Junior Boys",
"cMatchNo": "4",
"cRound": "First Round",
"nScheduledDate": "2016-12-11",
"nScheduledTime": "11:30:00",
"teamname1": "MOTHER SCHOOL",
"teamname2": "HARI SHEWA SCHOOL",
"nVenueID": "1",
"nTableID": "1",
"nTeamID1": "3",
"nTeamID2": "4",
"nTournamentID": "1",
"nFixtureDetailsID": "14",
"nEventTypeID": "5",
"image": "http://example.com/tt.png"
}
]
}
],
"live_score_international": [
{
"nTournamentID": "2",
"cTournamentName": "International Tournament Sample",
"cTournamentType": "I",
"dStartDate": "2016-12-22",
"dEndDate": "2016-12-24",
"matches": []
}
],
"results_domestic": [
{
"nTournamentID": "1",
"cTournamentName": "sample tournament.",
"cTournamentType": "D",
"dStartDate": "2016-12-10",
"dEndDate": "2016-12-12",
"matches": [
{
"cVenueCode": "TTAB",
"cTableName": "Table 1",
"cEventType": "Junior Boys",
"cMatchNo": "5",
"cRound": "First Round",
"nScheduledDate": "2016-12-11",
"nScheduledTime": "11:45:00",
"teamname1": "MOTHER SCHOOL",
"teamname2": "HARI SHEWA SCHOOL",
"nVenueID": "1",
"nTableID": "1",
"nTeamID1": "3",
"nTeamID2": "4",
"nTournamentID": "1",
"nFixtureDetailsID": "15",
"nEventTypeID": "5",
"image": "http://example.com/tt.png"
},
{
"cVenueCode": "TTAB",
"cTableName": "Table 1",
"cEventType": "Junior Boys",
"cMatchNo": "4",
"cRound": "First Round",
"nScheduledDate": "2016-12-11",
"nScheduledTime": "11:30:00",
"teamname1": "MOTHER SCHOOL",
"teamname2": "HARI SHEWA SCHOOL",
"nVenueID": "1",
"nTableID": "1",
"nTeamID1": "3",
"nTeamID2": "4",
"nTournamentID": "1",
"nFixtureDetailsID": "14",
"nEventTypeID": "5",
"image": "http://example.com/tt.png"
}
]
}
],
"results_international": [
{
"nTournamentID": "2",
"cTournamentName": "International Tournament Sample",
"cTournamentType": "I",
"dStartDate": "2016-12-22",
"dEndDate": "2016-12-24",
"matches": []
}
],
"fixture_point_domestic": [
{
"nTournamentID": "1",
"cTournamentName": "sample tournament.",
"cTournamentType": "D",
"dStartDate": "2016-12-10",
"dEndDate": "2016-12-12"
}
],
"fixture_point_international": [
{
"nTournamentID": "2",
"cTournamentName": "International Tournament Sample",
"cTournamentType": "I",
"dStartDate": "2016-12-22",
"dEndDate": "2016-12-24"
}
]
}
This is the fetch result.I want to render first a heading like domestic Tournaments.Then i want to loop for al tornaments in domestic.Then all matches in each tournament.How i can do this?Anyone to help.thanks in advance :)
can you please help me to figure out how to iterate all

First of all you should decide on how the data should be displayed on the screen. Should they be clickable or not, scrolled or not, what part of screen they should take etc. And depending on the visual design of your future application you can make a choice what exact react-native visual component will represent the data on the screen by the best way.
For example, if it is applicable by visual design of your application you can take a look at the react-native ListView or ScrollView components to render endless list of identical complex data.

If iterating through that entire JSON object is your intent, then an easy approach is a for...in loop.
For example:
var obj = {a: 1, b: 2, c: {a: 1, b: 2}};
function walk(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var val = obj[key];
console.log(val);
walk(val);
}
}
}
walk(obj);
Taken from: iterating through json object javascript

Object.keys(data).map((key) => { ... })
This will use the object properties (live_score_domestic, live_score_international, etc) to iterate through the top most data. Then you can use data[key] to get throught its contents.

Related

Setting up Continuous export of API data to csv for Racing lap times

Have access to an API that provides data for racing, including driver names, and their last lap, best lap.. etc. Completely new to coding but learning the ropes. This is an example of an output from the API.
"Successful": true,
"Session": {
"RunNumber": "47",
"SessionName": "KART DRIVERS - EXPERIENCE / PROVA 11 22:30",
"TrackName": "KGV RACE TRACKS - CIRCUITO 109",
"TrackLength": "0.725",
"CurrentTime": "23:05:24",
"SessionTime": "00:13:27",
"TimeToGo": "00:04:32",
"LapsToGo": "9999",
"FlagStatus": "Green",
"SortMode": "race",
"Classes": {
"1": {
"ClassID": "1",
"Description": "RENTAL"
}
},
"Competitors": {
"018": {
"RacerID": "018",
"Number": "018",
"Transponder": "02",
"FirstName": "LR",
"LastName": "",
"Nationality": "",
"AdditionalData": "",
"ClassID": "1",
"Position": "28",
"Laps": "8",
"TotalTime": "00:12:34.376",
"BestPosition": "26",
"BestLap": "8",
"BestLapTime": "00:01:09.158",
"LastLapTime": "00:01:09.158"
},
"043": {
"RacerID": "043",
"Number": "043",
"Transponder": "48",
"FirstName": "LORENZO",
"LastName": "",
"Nationality": "",
"AdditionalData": "",
"ClassID": "1",
"Position": "32",
"Laps": "5",
"TotalTime": "00:12:54.095",
"BestPosition": "32",
"BestLap": "4",
"BestLapTime": "00:01:38.740",
"LastLapTime": "00:02:39.277"
How would I go about reading data from this api every 30 seconds to 1 minute, and exporting that data into a CSV to put on excel?

Filtering out objects from multiple arrays in a JSONB column

I have a JSON structure with two arrays saved in a JSONB column. A bit simplified it looks like this
{
"prop1": "abc",
"prop2": "xyz",
"items": [
{
"itemId": "123",
"price": "10.00"
},
{
"itemId": "124",
"price": "9.00"
},
{
"itemId": "125",
"price": "8.00"
}
],
"groups": [
{
"groupId": "A",
"discount": "20",
"discountId": "1"
},
{
"groupId": "B",
"discount": "30",
"discountId": "2"
},
{
"groupId": "B",
"discount": "20",
"discountId": "3"
},
{
"groupId": "C",
"discount": "40",
"discountId": "4"
}
]
}
Schema:
CREATE TABLE campaign
(
id TEXT PRIMARY KEY,
data JSONB
);
Since each row (data column) can be fairly large, I'm trying to filter out matching item objects and group objects from the items and groups arrays.
My current query is this
SELECT * FROM campaign
WHERE
(data -> 'items' #> '[{"productId": "123"}]') OR
(data -> 'groups' #> '[{"groupId": "B"}]')
which returns rows containing either the matching group or the matching item. However, depending on the row, the data column can be a fairly large JSON object (there may be hundreds of objects in items and tens in groups and I've omitted several keys/properties for brevity in this example) which is affecting query performance (I've added GIN indexes on the items and groups arrays, so missing indices is not why it's slow).
How can I filter out the items and groups arrays to only contain matching elements?
Given this matching row
{
"prop1": "abc",
"prop2": "xyz",
"items": [
{
"itemId": "123",
"price": "10.00"
},
{
"itemId": "124",
"price": "9.00"
},
{
"itemId": "125",
"price": "8.00"
}
],
"groups": [
{
"groupId": "A",
"discount": "20",
"discountId": "1"
},
{
"groupId": "B",
"discount": "30",
"discountId": "2"
},
{
"groupId": "B",
"discount": "20",
"discountId": "3"
},
{
"groupId": "C",
"discount": "40",
"discountId": "4"
}
]
}
I'd like the result to be something like this (the matching item/group could be in different columns from the rest of the data column - doesn't have to be returned in a single JSON object with two arrays like this, but I would prefer it if doesn't affect performance or lead to a really hairy query):
{
"prop1": "abc",
"prop2": "xyz",
"items": [
{
"itemId": "123",
"price": "10.00"
}
],
"groups": [
{
"groupId": "B"
"discount": "20",
"discountId": "3"
}
]
}
What I've managed to do so far is unwrap and match an object in the items array using this query, which removes the 'items' array from the data column and filters out the matching item object to a separate column, but I'm struggling to join this with matches in the groups array.
SELECT data - 'items', o.obj
FROM campaign c
CROSS JOIN LATERAL jsonb_array_elements(c.data #> '{items}') o(obj)
WHERE o.obj ->> 'productId' = '124'
How can I filter both arrays in one query?
Bonus question: For the groups array I also want to return the object with the lowest discount value if possible. Or else the result would need to be an array of matching group objects instead of a single matching group.
Related questions: How to filter jsonb array elements and How to join jsonb array elements in Postgres?
If your postgres version is 12 or more, you can use the jsonpath language and functions. The query below returns the expected result with the subset of items and groups which match the given criteria. Then you can adapt this query within a sql function so that the search criteria is an input parameter.
SELECT jsonb_set(jsonb_set( data
, '{items}'
, jsonb_path_query_array(data, '$.items[*] ? (#.itemId == "123" && #.price == "10.00")'))
, '{groups}'
, jsonb_path_query_array(data, '$.groups[*] ? (#.groupId == "B" && #.discount == "20" && #.discountId == "3")'))
FROM (SELECT
'{
"prop1": "abc",
"prop2": "xyz",
"items": [
{
"itemId": "123",
"price": "10.00"
},
{
"itemId": "124",
"price": "9.00"
},
{
"itemId": "125",
"price": "8.00"
}
],
"groups": [
{
"groupId": "A",
"discount": "20",
"discountId": "1"
},
{
"groupId": "B",
"discount": "30",
"discountId": "2"
},
{
"groupId": "B",
"discount": "20",
"discountId": "3"
},
{
"groupId": "C",
"discount": "40",
"discountId": "4"
}
]
}' :: jsonb) AS d(data)
WHERE jsonb_path_exists(data, '$.items[*] ? (#.itemId == "123" && #.price == "10.00")')
AND jsonb_path_exists(data, '$.groups[*] ? (#.groupId == "B" && #.discount == "20" && #.discountId == "3")')

DocuSign API explorer - does not recognize tabs for Signature

I'm using the DocuSign API explorer to test the use of tabs for signature. I've created a Word document with a placeholder called /InternSignatureAnchor/ enter image description here
And the JSON request payload looks like this:
{
"documents": [
{
"documentBase64": "<Base64BytesHere>",
"documentId": "123456",
"fileExtension": "DOCX",
"name": "Freelance Contract"
}
],
"emailSubject": "Please Sign Contracts 3",
"recipients": {
"signers": [
{
"email": "dummy_email123#mmmm.mmm",
"name": "Dummy Recipient 1",
"recipientId": "1",
"roleName": "Internal Signatory",
"routingOrder": "1",
"tabs": {
"signHereTabs": [
{
"documentId": "123456",
"pageNumber": "1",
"tabLabel": "/InternSignatureAnchor/",
"xPosition": "0",
"yPosition": "36"
}
]
}
},
{
"email": "dummy_email321#mmmmm.mmm",
"name": "Dummy recipient 2",
"recipientId": "2",
"roleName": "External Signatory",
"routingOrder": "2"
}
]
},
"status": "sent"
}
When the signatory opens the document to sign, the "Sign" enter image description here
is not placed at the placeholder /InternSignatureAnchor/. When I use the Autoplace feature in DocuSign itself, it does work.
Anyone an idea?
Thanks in advance.
The "tagLabel" will just add a label to a tag. To use AutoTagging you'd need to use a json like below:
"tabs": {
"signHereTabs": [{
"anchorString": "Please Sign Here",
"anchorXOffset": "1",
"anchorYOffset": "0",
"anchorIgnoreIfNotPresent": "false",
"anchorUnits": "inches"
}]
}
I believe API explorer does not show the fields for auto tagging currently - so you'd need to perhaps use Postman to play around with this feature.

Karate - To find array indices of multiple occurance of an element in a JSON response array

I have a JSON response from an endpoint that gives me a nested array of elements. And given an input value, I want to find out all the index values where this value occurs and not just the first occurance of the element.
For example here is the response I have:
{
"items": [
{
"vin": "MMTestingVIN00002",
"dealerCode": "1",
"nmscCode": "1",
"warning": {
"warningLightType": {
"code": 1,
"description": "",
"symbol": "OLW",
"type": "S",
"priority": "1"
}
}
},
{
"vin": "HESQM0IBWUR7DH0DU",
"dealerCode": "1",
"nmscCode": "1",
"warning": {
"warningLightType": {
"code": 1,
"description": "",
"symbol": "OLW",
"type": "S",
"priority": "1"
}
}
},
{
"vin": "MMTestingVIN00002",
"dealerCode": "1",
"nmscCode": "1",
"warning": {
"warningLightType": {
"code": 1,
"description": "",
"symbol": "OLW",
"type": "S",
"priority": "1"
}
}
},
{
"vin": "ZCADWKEQM1GEADEQR",
"dealerCode": "1",
"nmscCode": "1",
"warning": {
"warningLightType": {
"code": 1,
"description": "",
"symbol": "WASH",
"type": "S",
"priority": "1"
}
}
},
{
"vin": "H5QGE06R54B8KYOUV",
"dealerCode": "1",
"nmscCode": "1",
"warning": {
"warningLightType": {
"code": 1,
"description": "",
"symbol": "WASH",
"type": "S",
"priority": "1"
}
}
}
]
}
I want to find out the indices where the arrays with "vin = MMTestingVIN00002" occurs.
I looked at https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature for ideas. Also looked at other SO answers and tried:
* def VIN = 'MMTestingVIN00002'
* def response = result.items
* def names = $[*].vin
* def index = names.indexOf(VIN)
* print index
This gives the only the first occurance with the index 0. Ideally I want a result array index[] which gives [0,2] as result.
Here you go:
* def indexes = []
* def fun = function(x, i){ if (x.vin == 'MMTestingVIN00002') karate.appendTo(indexes, i) }
* karate.forEach(response.items, fun)

Dynamic object key to display *ngFor in Nativescript with Angular

I have the dataset which has derived from the group by product name as below. I need to iterate the array of object and display the product name with all the size of the product.
Sample dataset
[ {
"TEST 1": [
{
"content_id": "88282",
"product_name": "TEST 1",
"price": "36.00",
"size" : "XL"
},
{
"content_id": "88283",
"product_name": "TEST 1",
"price": "37.00",
"size" : "XXL"
}
}],
{
"TEST 2": [
{
"content_id": "882821",
"product_name": "TEST 2",
"price": "36.00",
"size" : "XL"
},
{
"content_id": "882832",
"product_name": "TEST 2",
"price": "37.00",
"size" : "XXL"
}]
]
I need to iterate the above result and need to display as Product name with all sizes such as "TEST 1 (XL XXL ) and TEST 2 (XL XXL)
What I am trying is
<----- need to display here --------->
You can use Object.keys() to get keys or use foreach() to iterate json.
E.g.
const object1 = {"data":
[ { "TEST 1": [ {
"content_id": "88282",
"product_name": "TEST 1",
"price": "36.00",
"size" : "XL"
}, {
"content_id": "88283",
"product_name": "TEST 1",
"price": "37.00",
"size" : "XXL"
}
]
}
]
};
object1[Object.keys(object1)[0]].forEach(function(element) {
console.log(element);
});