I want to have the JSON below as an array with duplicated key values, i.e.:
"2016-09-16":{"available":"1","bind":0,"info":"","notes":"","price":"","promo":"","status":"booked"}
twice. How can I do that?
{
"2016-06-28": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-06-29": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-06-30": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-07-04": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-07-05": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-07-06": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-07-07": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-09-16": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-09-15": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-09-14": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-09-13": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-09-16": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
},
"2016-09-17": {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
}
}
JSON with duplicate keys in the same object is not reliable across JSON parsers (some will choke, some will give you only the value of the last occurrence) and not useful in any case. Use arrays of objects for the values of those date keys, not individual objects:
{
"2016-06-29": [{
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
}],
"2016-09-16": [{
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
}, {
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
}],
"2016-09-15": [{
"available": "1",
"bind": 0,
"info": "",
"notes": "",
"price": "",
"promo": "",
"status": "booked"
}]
}
(Data shortened for clarity.)
In the above, note how 2016-06-29 and 2016-09-15 have arrays with just one entry, but 2016-09-16 has an array with two entries.
Related
[
{
"id": 0.5256669517010202,
"color": false,
"selected": false,
"name": "",
"type": "",
"label": "",
"fieldName": "",
"required": "",
"validation": ""
},
{
"id": 0.5901705709044824,
"color": false,
"selected": false,
"type": [
{
"id": 0.30332161644408817,
"color": true,
"selected": false,
"name": "",
"type": "",
"label": "",
"fieldName": "",
"required": "",
"validation": ""
},
{
"id": 0.5423422175390649,
"color": true,
"selected": false,
"name": "",
"type": "",
"label": "",
"fieldName": "",
"required": "",
"validation": ""
},
{
"id": 0.959208393000617,
"color": true,
"selected": false,
"name": "",
"type": "",
"label": "",
"fieldName": "",
"required": "",
"validation": ""
}
],
"label": "",
"fieldName": "",
"required": "",
"validation": ""
},
{
"id": 0.5933110602496239,
"color": false,
"selected": false,
"type": "",
"label": "",
"fieldName": "",
"required": "",
"validation": ""
}
]
I think you should use Map method
const data = [
{
"id": 0.5256669517010202,
"color": false,
"selected": false,
"name": "",
"type": "",
"label": "",
"fieldName": "",
"required": "",
"validation": ""
},
{
"id": 0.5901705709044824,
"color": false,
"selected": false,
"type": [
{
"id": 0.30332161644408817,
"color": true,
"selected": false,
"name": "",
"type": "",
"label": "",
"fieldName": "",
"required": "",
"validation": ""
},
{
"id": 0.5423422175390649,
"color": true,
"selected": false,
"name": "",
"type": "",
"label": "",
"fieldName": "",
"required": "",
"validation": ""
},
{
"id": 0.959208393000617,
"color": true,
"selected": false,
"name": "",
"type": "",
"label": "",
"fieldName": "",
"required": "",
"validation": ""
}
],
"label": "",
"fieldName": "",
"required": "",
"validation": ""
},
{
"id": 0.5933110602496239,
"color": false,
"selected": false,
"type": "",
"label": "",
"fieldName": "",
"required": "",
"validation": ""
}
]
data.map((item) => item.id)
Just Try!
You should declared attribute type as Array always!
data.map(Item => (
Item.type.map(Item => ())
))
the map method is suitable here. For example you have your all data within state or another variable, well you can map throw it:
<div>
{yourData?.length && yourData.map(item => {
return(
<span>{item.id}</span>
...
<div>{item.type.map(typeItem => {
return(
<span>{typeItem.id}</span>
...
)
})
)})}
</div>
We have the following JSON structure from an TSheets API which has an actual timesheet 'id' as an object in the hierarchy. This means there's no fixed hierarchy structure and we need to dynamically find a way to loop through each timesheet.
We've stored this data in a variant column, but want to flatten it and have a row per timesheet. Is there a way to list all objects under results.timesheets to retrieve all ids in a single column (i.e. '13510958','13510960') so we can loop through these to obtain the lower level details. Seems like an odd way to construct an API response!
JSON can be found below:
{
"results": {
"timesheets": {
"13510958": {
"id": 13510958,
"user_id": 1360082,
"jobcode_id": 16297998,
"start": "",
"end": "",
"duration": 28800,
"date": "2021-03-29",
"tz": 1,
"tz_str": "Europe/London",
"type": "manual",
"location": "QuickBooks Time web",
"on_the_clock": false,
"locked": 0,
"notes": "",
"customfields": {
"802478": "",
"650642": "",
"650640": "Consulting Services:Services"
},
"last_modified": "2021-04-19T14:34:16+00:00",
"attached_files": [],
"created_by_user_id": 1360067
},
"13510960": {
"id": 13510960,
"user_id": 1360082,
"jobcode_id": 16297998,
"start": "",
"end": "",
"duration": 28800,
"date": "2021-03-30",
"tz": 1,
"tz_str": "Europe/London",
"type": "manual",
"location": "QuickBooks Time web",
"on_the_clock": false,
"locked": 0,
"notes": "",
"customfields": {
"802478": "",
"650642": "",
"650640": "Consulting Services:Services"
},
"last_modified": "2021-04-19T14:34:16+00:00",
"attached_files": [],
"created_by_user_id": 1360067
}}
} }
You can use LATERL FLATTEN, LISTAGG or ARRAY_AGG to get it:
with json_data as ( select parse_json('{
"results": {
"timesheets": {
"13510958": {
"id": 13510958,
"user_id": 1360082,
"jobcode_id": 16297998,
"start": "",
"end": "",
"duration": 28800,
"date": "2021-03-29",
"tz": 1,
"tz_str": "Europe/London",
"type": "manual",
"location": "QuickBooks Time web",
"on_the_clock": false,
"locked": 0,
"notes": "",
"customfields": {
"802478": "",
"650642": "",
"650640": "Consulting Services:Services"
},
"last_modified": "2021-04-19T14:34:16+00:00",
"attached_files": [],
"created_by_user_id": 1360067
},
"13510960": {
"id": 13510960,
"user_id": 1360082,
"jobcode_id": 16297998,
"start": "",
"end": "",
"duration": 28800,
"date": "2021-03-30",
"tz": 1,
"tz_str": "Europe/London",
"type": "manual",
"location": "QuickBooks Time web",
"on_the_clock": false,
"locked": 0,
"notes": "",
"customfields": {
"802478": "",
"650642": "",
"650640": "Consulting Services:Services"
},
"last_modified": "2021-04-19T14:34:16+00:00",
"attached_files": [],
"created_by_user_id": 1360067
}}
}}') raw )
select listagg( v.key , ',' ), array_agg( v.key)
from json_data,
lateral flatten( raw:results.timesheets ) v;
When you want to obtain the lower level details without looping through them, you can also access them directly. For example the timesheet, user_id and duration:
with json_data as (
select parse_json('{
"results": {
"timesheets": {
"13510958": {
"id": 13510958,
"user_id": 1360082,
"jobcode_id": 16297998,
"start": "",
"end": "",
"duration": 28800,
"date": "2021-03-29",
"tz": 1,
"tz_str": "Europe/London",
"type": "manual",
"location": "QuickBooks Time web",
"on_the_clock": false,
"locked": 0,
"notes": "",
"customfields": {
"802478": "",
"650642": "",
"650640": "Consulting Services:Services"
},
"last_modified": "2021-04-19T14:34:16+00:00",
"attached_files": [],
"created_by_user_id": 1360067
},
"13510960": {
"id": 13510960,
"user_id": 1360082,
"jobcode_id": 16297998,
"start": "",
"end": "",
"duration": 28800,
"date": "2021-03-30",
"tz": 1,
"tz_str": "Europe/London",
"type": "manual",
"location": "QuickBooks Time web",
"on_the_clock": false,
"locked": 0,
"notes": "",
"customfields": {
"802478": "",
"650642": "",
"650640": "Consulting Services:Services"
},
"last_modified": "2021-04-19T14:34:16+00:00",
"attached_files": [],
"created_by_user_id": 1360067
}}
}}') raw )
select key, value:user_id, value:duration
from json_data,
lateral flatten(input=>raw:results.timesheets)
I was able to get this to work.
SELECT VALUE AS TIMESHEET_JSON, TIMESHEET_JSON:id AS ID
FROM TABLE(FLATTEN(INPUT=> PARSE_JSON('{Your JSON Here}'):results.timesheets));
You may need to do a little more work to get your JSON into the flatten. A lateral flatten may be useful. This worked once I put your JSON into a one column cte.
SELECT VALUE AS TIMESHEET_JSON, TIMESHEET_JSON:id AS ID
FROM cte
,LATERAL FLATTEN(INPUT=> PARSE_JSON(JSON):results.timesheets);
You can then use typical JSON parsing syntax against the VALUE column for getting at your individual attributes per record.
This is an input that needs to be transformed using Jolt Transformation to obtain the expected output.
I am trying to write a jolt transformation for the below input.
but It's not matched with the expected output.
My Input:
{
"ConsolidatedList": [
{
"AliasType": "AKA",
"AliasTypeName": "AKA",
"BusinessRegNumber": {
"nil": null
},
"nil": null,
"Country": null,
"CountryOfBirth": "Syria",
"CurrentOwners": {
"nil": null
},
"DateListed": "2021-03-15T00:00:00",
"DateListedDay": 15,
"DateListedMonth": 3,
"DateListedYear": 2021,
"DateOfBirth": "01/09/1975",
"DateOfBirthId": 4751,
"DayOfBirth": 1,
"EmailAddress": {
"nil": null
},
"FCOId": "SYR0377",
"FlagOfVessel": {
"nil": null
},
"FullAddress": null,
"FullName": " Al Shebil Luna ",
"FurtherIdentifiyingInformation": {
"nil": null
},
"Gender": "Female",
"GroupID": 14070,
"GroupStatus": "Asset Freeze Targets",
"GroupTypeDescription": "Individual",
"GrpStatus": "A",
"HIN": {
"nil": null
},
"ID": 33233,
"IMONumber": {
"nil": null
},
"LastUpdated": "2021-03-15T00:00:00",
"LastUpdatedDay": 15,
"LastUpdatedMonth": 3,
"LastUpdatedYear": 2021,
"LengthOfVessel": {
"nil": null
},
"ListingType": "UK autonomous",
"MonthOfBirth": 9,
"Name6": "Al Shebil",
"NameTitle": null,
"NationalIdNumber": {
"nil": null
},
"Nationality": "Syria",
"OrgType": {
"nil": null
},
"OtherInformation": "MapRecord[{{http://www.w3.org/2001/XMLSchema-instance}nil=true}]",
"ParentCompany": {
"nil": null
},
"PassportDetails": "MapRecord[{{http://www.w3.org/2001/XMLSchema-instance}nil=true}]",
"PhoneNumber": {
"nil": null
},
"Position": "Media Adviser to President Assad",
"PostCode": null,
"PreviousFlags": {
"nil": null
},
"PreviousOwners": {
"nil": null
},
"RegimeName": "Syria",
"Subsidiaries": {
"nil": null
},
"TonnageOfVessel": {
"nil": null
},
"TownOfBirth": "Suweida",
"TypeOfVessel": {
"nil": null
},
"UKStatementOfReasons": "Adviser to President Assad and a prominent member of his inner circle. As Media Adviser to the President she supports the Syrian regime, which relies on disinformation and a lack of media freedom to repress the civilian population. She is also associated with the Syrian regime through her role as an adviser.\n",
"Website": {
"nil": null
},
"YearBuilt": {
"nil": null
},
"YearOfBirth": 1975,
"address1": null,
"address2": null,
"address3": null,
"address4": null,
"address5": null,
"address6": null,
"name1": "Luna",
"name2": null,
"name3": null,
"name4": null,
"name5": null
},
{
"AliasType": "AKA",
"AliasTypeName": "AKA",
"BusinessRegNumber": {
"nil": null
},
"nil": null,
"Country": null,
"CountryOfBirth": "Syria",
"CurrentOwners": {
"nil": null
},
"DateListed": "2021-03-15T00:00:00",
"DateListedDay": 15,
"DateListedMonth": 3,
"DateListedYear": 2021,
"DateOfBirth": "01/09/1975",
"DateOfBirthId": 4751,
"DayOfBirth": 1,
"EmailAddress": {
"nil": null
},
"FCOId": "SYR0377",
"FlagOfVessel": {
"nil": null
},
"FullAddress": null,
"FullName": " Al Shebil Luna ",
"FurtherIdentifiyingInformation": {
"nil": null
},
"Gender": "Female",
"GroupID": 14070,
"GroupStatus": "Asset Freeze Targets",
"GroupTypeDescription": "Individual",
"GrpStatus": "A",
"HIN": {
"nil": null
},
"ID": 33233,
"IMONumber": {
"nil": null
},
"LastUpdated": "2021-03-15T00:00:00",
"LastUpdatedDay": 15,
"LastUpdatedMonth": 3,
"LastUpdatedYear": 2021,
"LengthOfVessel": {
"nil": null
},
"ListingType": "UK autonomous",
"MonthOfBirth": 9,
"Name6": "Al Shebil",
"NameTitle": null,
"NationalIdNumber": {
"nil": null
},
"Nationality": "Syria",
"OrgType": {
"nil": null
},
"OtherInformation": "MapRecord[{{http://www.w3.org/2001/XMLSchema-instance}nil=true}]",
"ParentCompany": {
"nil": null
},
"PassportDetails": "MapRecord[{{http://www.w3.org/2001/XMLSchema-instance}nil=true}]",
"PhoneNumber": {
"nil": null
},
"Position": "Media Adviser to President Assad",
"PostCode": null,
"PreviousFlags": {
"nil": null
},
"PreviousOwners": {
"nil": null
},
"RegimeName": "Syria",
"Subsidiaries": {
"nil": null
},
"TonnageOfVessel": {
"nil": null
},
"TownOfBirth": "Suweida",
"TypeOfVessel": {
"nil": null
},
"UKStatementOfReasons": "Adviser to President Assad and a prominent member of his inner circle. As Media Adviser to the President she supports the Syrian regime, which relies on disinformation and a lack of media freedom to repress the civilian population. She is also associated with the Syrian regime through her role as an adviser.\n",
"Website": {
"nil": null
},
"YearBuilt": {
"nil": null
},
"YearOfBirth": 1975,
"address1": null,
"address2": null,
"address3": null,
"address4": null,
"address5": null,
"address6": null,
"name1": "Hassan",
"name2": "Muh",
"name3": "Cane",
"name4": null,
"name5": null
}
]
}
And the expected output is -
{
"Count": "2", // Counter for the number of records
"LastUpdated": "08/03/2020 12:00:00",
"RecordType": [
"Entity",
"Individual",
"Vessel"
],
"Pages": "1",
"Records": [
{
"_id": "1", // Counter
"AdditionalInformation": "Other Information: (UK Sanctions List Ref):AQD0081 (UN Ref):QDe.014 (Further Identifiying Information):Headed by Abdelmalek Droukdel (QDi.232).Zone of operation includes Algeria and parts of Mali, Mauritania, Niger, Tunisia and Morocco.Review pursuant to Security Council resolution 1822 (2008) was concluded on 21 Jun.2010.INTERPOL-UN Security Council Special Notice web link: https://www.interpol.int/en/notice/search/une/5278467; Listed On: 10/10/2001; Last Updated: 12/31/2020; Group ID: 7247",
"RecordType": "Entity",
"Names": [
{
"_id": "1", //counter
"OtherName": "THE ORGANIZATION OF AL-QAIDA IN THE ISLAMIC MAGHREB",
"UnparsedName": "",
"FirstName": "",
"LastName": "",
"NameType": "Primary"
},
{
"_id": "2",
"OtherName": "AL QAIDA AU MAGHREB ISLAMIQUE (AQMI)",
"UnparsedName": "",
"FirstName": "",
"LastName": "",
"NameType": "Alias"
},
{
"_id": "3",
"OtherName": "AQIM",
"UnparsedName": "",
"FirstName": "",
"LastName": "",
"NameType": "Alias"
},
{
"_id": "4",
"OtherName": "LE GROUPE SALAFISTE POUR LA PREDICATION ET LE COMBAT (GSPC)",
"UnparsedName": "",
"FirstName": "",
"LastName": "",
"NameType": "Formerly Known As"
},
{
"_id": "5",
"OtherName": "SALAFIST GROUP FOR CALL AND COMBAT",
"UnparsedName": "",
"FirstName": "",
"LastName": "",
"NameType": "Formerly Known As"
}
],
"Addresses": [
{
"_id": "1",
"PostalCode": "",
"Street": "",
"City": "",
"StateProvince": "",
"Country": {
"Name": "Algeria",
"RelationType": "Address"
}
},
{
"_id": "2",
"PostalCode": "",
"Street": "",
"City": "",
"StateProvince": "",
"Country": {
"Name": "Mali",
"RelationType": "Address"
}
},
{
"_id": "3",
"PostalCode": "",
"Street": "",
"City": "",
"StateProvince": "",
"Country": {
"Name": "Mauritania",
"RelationType": "Address"
}
},
{
"_id": "4",
"PostalCode": "",
"Street": "",
"City": "",
"StateProvince": "",
"Country": {
"Name": "Morocco",
"RelationType": "Address"
}
},
{
"_id": "5",
"PostalCode": "",
"Street": "",
"City": "",
"StateProvince": "",
"Country": {
"Name": "Niger",
"RelationType": "Address"
}
},
{
"_id": "6",
"PostalCode": "",
"Street": "",
"City": "",
"StateProvince": "",
"Country": {
"Name": "Tunisia",
"RelationType": "Address"
}
}
],
"Sanctions": [
{
"_id": "1",
"Sanction": "HM Treasury Al-Qaida Sanction",
"ExpirationDate": "",
"SanctionDate": ""
}
]
},
{
"_id": "2",
"AdditionalInformation": "Other Information: (UK Sanctions List Ref):AFG0110 (UN Ref): TAi.142 (Further Identifiying Information):Review pursuant to Security Council resolution 1822 (2008) was concluded on 29 Jul. 2010. INTERPOL-UN Security Council Special Notice web link: https://www.interpol.int/en/How-we-work/Notices/View-UN-Notices-Individuals click here.; Listed On: 02/23/2001; Last Updated: 2/1/2021; Group ID: 6895",
"RecordType": "Person",
"Names": [
{
"_id": "1",
"OtherName": "",
"UnparsedName": "",
"FirstName": "Abdul Hai Hazem",
"LastName": "ABDUL QADER",
"NameType": "Primary"
},
{
"_id": "2",
"OtherName": "",
"UnparsedName": "",
"FirstName": "Abdul Hai",
"LastName": "HAZEM",
"NameType": "Alias"
}
],
"Identifiers": [
{
"_id": "1",
"IDValue": "1971",
"IDType": "Date Of Birth"
},
{
"_id": "2",
"IDValue": "Pashawal Yargatoo village, Andar District, Ghazni Province, Afghanistan",
"IDType": "Place Of Birth"
},
{
"_id": "3",
"IDValue": "D 0001203 (Afghan)",
"IDType": "Passport"
}
],
"Addresses": [
{
"_id": "1",
"PostalCode": "",
"Street": "",
"City": "",
"StateProvince": "Kabul Province",
"Country": {
"Name": "Afghanistan",
"RelationType": "Address"
}
},
{
"_id": "2",
"PostalCode": "",
"Street": "",
"City": "Kabul City",
"StateProvince": "Kabul Province",
"Country": {
"Name": "",
"RelationType": "Address"
}
},
{
"_id": "3",
"PostalCode": "",
"Street": "",
"City": "",
"StateProvince": "",
"Country": {
"Name": "Afghan",
"RelationType": "Nationality"
}
}
],
"Sanctions": [
{
"_id": "1",
"Sanction": "HM Treasury Afghanistan Sanction",
"ExpirationDate": "",
"SanctionDate": ""
}
]
}
]
}
My spec is -
[
{
"operation" : "shift",
"spec": {
"ConsolidatedList": {
"*": {
"GroupTypeDescription": "RecordType",
"LastUpdated": "LastUpdated",
"UKStatementOfReasons": "Records.AdditionalInformation",
"Name6": "Records.Names.[&1].Other Name",
"name3": "Records.Names.[&1].UnparsedName",
"name1": "Records.Names.[&1].FirstName",
"name2": "Records.Names.[&1].LastName",
"AliasType": "Records.Names.[&1].NameType",
"PostCode": "Records.Address.[&1].PostalCode",
"address2": "Records.Address.[&1].Street",
"address6": "Records.Address.[&1].City",
"Country": "Records.Address.[&1].StateProvince",
"CountryOfBirth": "Records.Address.[&1].Country.Name"
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec" : {
"Count": "size(1,Records)",
"Pages": "1"
}
}
]
The spec is not transforming as expected output.
I have been working on a COVID-19 dashboard where I want to display the daily cases and daily deaths in the line graph. I can get the data from the following api https://api.covid19api.com/total/dayone/country/United%20Kingdom but it return me the total commutative cases and total commutative deaths on the given date. The data returned is in the following structure
const data = [
{
"Country": "United Kingdom",
"CountryCode": "",
"Province": "",
"City": "",
"CityCode": "",
"Lat": "0",
"Lon": "0",
"Confirmed": 190584,
"Deaths": 28734,
"Recovered": 0,
"Active": 161850,
"Date": "2020-05-04T00:00:00Z"
},
{
"Country": "United Kingdom",
"CountryCode": "",
"Province": "",
"City": "",
"CityCode": "",
"Lat": "0",
"Lon": "0",
"Confirmed": 194990,
"Deaths": 29427,
"Recovered": 0,
"Active": 165563,
"Date": "2020-05-05T00:00:00Z"
},
{
"Country": "United Kingdom",
"CountryCode": "",
"Province": "",
"City": "",
"CityCode": "",
"Lat": "0",
"Lon": "0",
"Confirmed": 201101,
"Deaths": 30076,
"Recovered": 0,
"Active": 171025,
"Date": "2020-05-06T00:00:00Z"
}
]
Where Confirmed is the total number of cases detected on that day. To calculate the daily cases on the specific day we can subtract the confirmed from the previous confirmed of previous date. For example 2020-05-05 the number of cases confirmed on that day can be calculated by subtracting the number of cases on 20-05-04 the previous day. So the calculation would be 194990- 190584 = 4442. For the first object of the array the value stays the same since there is no previous record to subtract from as I will be fetching the day one when a case was detected in that country. Therefore the desired data would be the following
const desiredData= [
{
"Country": "United Kingdom",
"CountryCode": "",
"Province": "",
"City": "",
"CityCode": "",
"Lat": "0",
"Lon": "0",
"Confirmed": 190584,
"Deaths": 28734,
"Recovered": 0,
"Active": 161850,
"Date": "2020-05-04T00:00:00Z"
},
{
"Country": "United Kingdom",
"CountryCode": "",
"Province": "",
"City": "",
"CityCode": "",
"Lat": "0",
"Lon": "0",
"Confirmed": 4442,
"Deaths": 29427,
"Recovered": 0,
"Active": 165563,
"Date": "2020-05-05T00:00:00Z"
},
{
"Country": "United Kingdom",
"CountryCode": "",
"Province": "",
"City": "",
"CityCode": "",
"Lat": "0",
"Lon": "0",
"Confirmed": 6111,
"Deaths": 30076,
"Recovered": 0,
"Active": 171025,
"Date": "2020-05-06T00:00:00Z"
}
]
I am using Vue.js I think this could be a basic javascript function which can loop through the array and subtract the "Confirmed" of current object from the "Confirmed" of previous object. Please do let me know how this can be acheived. Many Thanks
You can use something like this:
result = data.map((record, index, allRecords) => {
const previous = allRecords[index - 1] || {
Confirmed: 0
}
return {
...record,
newConfirmed: record.Confirmed - previous.Confirmed
}
});
Which will give you a result such as this which includes the "newConfirmed" key and value:
[{
"Country": "United Kingdom",
"CountryCode": "",
"Province": "",
"City": "",
"CityCode": "",
"Lat": "0",
"Lon": "0",
"Confirmed": 190584,
"Deaths": 28734,
"Recovered": 0,
"Active": 161850,
"Date": "2020-05-04T00:00:00Z",
"newConfirmed": 190584
}, {
"Country": "United Kingdom",
"CountryCode": "",
"Province": "",
"City": "",
"CityCode": "",
"Lat": "0",
"Lon": "0",
"Confirmed": 194990,
"Deaths": 29427,
"Recovered": 0,
"Active": 165563,
"Date": "2020-05-05T00:00:00Z",
"newConfirmed": 4406
}, {
"Country": "United Kingdom",
"CountryCode": "",
"Province": "",
"City": "",
"CityCode": "",
"Lat": "0",
"Lon": "0",
"Confirmed": 201101,
"Deaths": 30076,
"Recovered": 0,
"Active": 171025,
"Date": "2020-05-06T00:00:00Z",
"newConfirmed": 6111
}]
How to fetch array of array data in angular 2.
My json data is as follows,
[[{
"pk_emp_id":5,
"tenant_id":"Zone1",
"location_id":1,
"emp_number":"sk44",
"prefix":"",
"first_name":"qqqqq",
"middle_name":"www",
"last_name":"eeee",
"display_name":"qqqq",
"full_name":"qqq qqqq",
"email":"qqqq#gmail.com",
"gender":"Female",
"emp_type_id":2,
"date_of_hire":191000,
"date_of_birth":null,
"manager_id":7,
"phone_number":"9877654",
"position":"SE",
"responsibility":"",
"notes":"",
"contracted":"0",
"street":"vidyanagar",
"state":"Karnataka",
"city":"hubli",
"zip_code":"9898",
"dob":-19800000,
"ssn":"",
"deleted":0
},{
"pk_empt_id":2,
"empt_tenant_id":"2",
"***empt_name***":"temporary",
"deleted":0
}]]
How can I fetch empt_name in datatable(row data)?
let dummyArr = [
[{
"pk_emp_id": 5,
"tenant_id": "Zone1",
"location_id": 1,
"emp_number": "sk44",
"prefix": "",
"first_name": "qqqqq",
"middle_name": "www",
"last_name": "eeee",
"display_name": "qqqq",
"full_name": "qqq qqqq",
"email": "qqqq#gmail.com",
"gender": "Female",
"emp_type_id": 2,
"date_of_hire": 191000,
"date_of_birth": null,
"manager_id": 7,
"phone_number": "9877654",
"position": "SE",
"responsibility": "",
"notes": "",
"contracted": "0",
"street": "vidyanagar",
"state": "Karnataka",
"city": "hubli",
"zip_code": "9898",
"dob": -19800000,
"ssn": "",
"deleted": 0
}, {
"pk_empt_id": 2,
"empt_tenant_id": "2",
"***empt_name***": "temporary",
"deleted": 0
}]
];
dummyArr.forEach(element =>{
element.forEach(element2=>{
console.log(element2)
})
});
I believe you have exposed a GET method in your web api
In Angular using HTTP service
this.http.get("").subscribe(result => {
conosle.log(result[1].empt_name) });
You can take it in an array and read it like this:
x = [
[{
"pk_emp_id": 5,
"tenant_id": "Zone1",
"location_id": 1,
"emp_number": "sk44",
"prefix": "",
"first_name": "qqqqq",
"middle_name": "www",
"last_name": "eeee",
"display_name": "qqqq",
"full_name": "qqq qqqq",
"email": "qqqq#gmail.com",
"gender": "Female",
"emp_type_id": 2,
"date_of_hire": 191000,
"date_of_birth": null,
"manager_id": 7,
"phone_number": "9877654",
"position": "SE",
"responsibility": "",
"notes": "",
"contracted": "0",
"street": "vidyanagar",
"state": "Karnataka",
"city": "hubli",
"zip_code": "9898",
"dob": -19800000,
"ssn": "",
"deleted": 0
}, {
"pk_empt_id": 2,
"empt_tenant_id": "2",
"***empt_name***": "temporary",
"deleted": 0
}]
]
$('#a').text(x[0][1]['***empt_name***'])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<span id='a'>sadsad </span>
This is just Javascript object manipulation.
You can do something like :
outerArray.forEach(innerArray =>{
if(innerArray.length){
innerArray.forEach(element =>{
//do somthing here with element.empt_name
console.log(element.empt_name);
});
}
})