How to compare rows from JSON file in angular? - json

I have this items in a JSON file that I need to compare wherever I have Curr or Prev values and compare if they are equals. this algorithm is with angular 5 but I'm being unable to do this, I did the load function with httpClient
Example :
[{
"id": 1,
"comparisonDate": "2017-06-22",
"milestone": "CURR_PREV",
"udDomain": "BASIS",
"udIdentifier": "332A251000.01",
"rpt": "D70",
"approvedCurr": true,
"approvedPrev": null,
"commercialReferenceCurr": "BASIS - 01.01.2007",
"commercialReferencePrev": null,
"customizationImpactCurr": null,
"customizationImpactPrev": null,
"deliveryTypeCurr": null,
"deliveryTypePrev": null,
"aircraftId": 184,
"aircraftSapId": "580652",
"aircraftName": "BUL 011",
"aircraftSerialNumber": "02713",
"quantityCurr": "1",
"quantityPrev": null,
"statusCurr": "O",
"statusPrev": null,
"typeModification": "CREATION",
"validityCurr": "V",
"validityPrev": null
}, {
"id": 2,
"comparisonDate": "2017-06-22",
"milestone": "CURR_PREV",
"udDomain": "BASIS",
"udIdentifier": "332A270028.01",
"rpt": "E15",
"approvedCurr": true,
"approvedPrev": null,
"commercialReferenceCurr": "BASIS - 01.01.2007",
"commercialReferencePrev": null,
"customizationImpactCurr": null,
"customizationImpactPrev": null,
"deliveryTypeCurr": null,
"deliveryTypePrev": null,
"aircraftId": 184,
"aircraftSapId": "580652",
"aircraftName": "BUL 011",
"aircraftSerialNumber": "02713",
"quantityCurr": "1",
"quantityPrev": null,
"statusCurr": "O",
"statusPrev": null,
"typeModification": "CREATION",
"validityCurr": "V",
"validityPrev": null
}]
this should be with using lodash.

i assume your want something like this
for each object in your array
filter keys that end with "Curr"
for each get Prev property
compare it with curr property
let obj = [{
"id": 1,
"comparisonDate": "2017-06-22",
"milestone": "CURR_PREV",
"udDomain": "BASIS",
"udIdentifier": "332A251000.01",
"rpt": "D70",
"approvedCurr": true,
"approvedPrev": null,
"commercialReferenceCurr": "BASIS - 01.01.2007",
"commercialReferencePrev": null,
"customizationImpactCurr": null,
"customizationImpactPrev": null,
"deliveryTypeCurr": null,
"deliveryTypePrev": null,
"aircraftId": 184,
"aircraftSapId": "580652",
"aircraftName": "BUL 011",
"aircraftSerialNumber": "02713",
"quantityCurr": "1",
"quantityPrev": null,
"statusCurr": "O",
"statusPrev": null,
"typeModification": "CREATION",
"validityCurr": "V",
"validityPrev": null
}, {
"id": 2,
"comparisonDate": "2017-06-22",
"milestone": "CURR_PREV",
"udDomain": "BASIS",
"udIdentifier": "332A270028.01",
"rpt": "E15",
"approvedCurr": true,
"approvedPrev": null,
"commercialReferenceCurr": "BASIS - 01.01.2007",
"commercialReferencePrev": null,
"customizationImpactCurr": null,
"customizationImpactPrev": null,
"deliveryTypeCurr": null,
"deliveryTypePrev": null,
"aircraftId": 184,
"aircraftSapId": "580652",
"aircraftName": "BUL 011",
"aircraftSerialNumber": "02713",
"quantityCurr": "1",
"quantityPrev": null,
"statusCurr": "O",
"statusPrev": null,
"typeModification": "CREATION",
"validityCurr": "V",
"validityPrev": null
}];
obj.forEach(x=>{
Object.keys(x)
.filter(y=>y.endsWith("Curr"))
.forEach(y=>{
let prevProp = y.replace("Curr", "Prev")
console.log(y, x[y]);
console.log(prevProp ,x[prevProp ]);
if(x[y]===x[prevProp ]){
//equal
}else{
//not equal
}
})
})
you could use lodash's _.forEach, _.Keys, _.filter but i think there is not much point on this.

Related

How to solve Cannot cast object '0.0' with class 'java.lang.Double' to class 'org.json.JSONObject'

This is some part of my input json :-
"orderLine": {
"agreementHeaderId": [
null
],
"agreementNumber": [
null
],
"customerProductId": null,
"displayLineNumber": "2",
"estimateFulfillmentCost": 9.97,
"extendedAmount": null,
"fulfilledQuantity": null,
"fulfillToleranceAbove": null,
"inventoryOrganizationCode": "107",
"inventoryOrganizationId": "107",
"jobName": null,
"orderedQuantity": 1.0,
"orderedUom": null,
"origSystemDocumentLineReference": "2",
"paymentTerms": "CASH ON DEMAND",
"paymentTermsCode": "COD",
"productId": "4065412*107",
"productNumber": "4065412",
"productDescription": "20 DFU AAV W/ 1-1/2-2 PVC ADPT",
"purchasingUom": null,
"quantityPerModel": 0.0,
"requestedShipDate": "2022-12-19T05:00:00.000Z",
"requestedSupplierNumber": null,
"requisitionInventoryOrganizationId": "107",
"rmaDeliveredQuantity": null,
"scheduleShipDate": null,
"shippedQuantity": 1.0,
"shippingCarrier": "OT",
"shippingInstructions": null,
"shippingMode": "OUR TRUCK",
"shippingModeCode": "OT",
"subInventory": null,
"transactionLineTypeCode": null,
"transactionCategoryCode": null,
"transactionLineType": "MP",
"unitListPrice": 33.23,
"unitQuantity": 1.0,
"unitSellingPrice": 44.0,
Few values are double type not string
So, while giving this JSONObject oldQty = (JSONObject) orderLine.get("orderedQuantity")
I'm getting Cannot cast object '0.0' with class 'java.lang.Double' to class 'org.json.JSONObject' this error

NestJs TypeOrm loadRelationCountAndMap apply order by operation on it

I am new in NestJs TypeORM and I tried to get the count only from the relation table using loadRelationCountAndMap method and now I need a fetuare to change the order of that count ASC or DESC but when I tried the orderBy method it will return me unknow colunm name.
Here is my query
await this.userRepository
.createQueryBuilder('user')
.leftJoinAndSelect('user.roles', 'roles')
.loadRelationCountAndMap('user.postCount', 'user.post', 'post', qb =>
qb.where('post.type =:type', { type: 'post' }),)
.loadRelationCountAndMap('user.userSpotCount', 'user.spot', 'spot')
.loadRelationCountAndMap(
'user.itineraryCount',
'user.post',
'post',
qb => qb.where('post.type =:type', { type: 'itinerary' }),)
.where('roles.name = :name', { name: request.user_type })
.orderBy('userSpotCount', 'ASC');
and the output is without orderBy method
[{
"id": xxxxx,
"name": "xxxx xxxx",
"email": "xxxx.xxxx#xxxx.xxx",
"facebook_id": null,
"google_id": null,
"apple_id": null,
"is_email_verifie": null,
"is_phone_verifie": null,
"created_at": "2022-06-06T23:38:04.000Z",
"updated_at": "2022-06-06T23:38:04.000Z",
"phone": "1234567890",
"token": null,
"profile_pic": "",
"status": "pending",
"email_token": null,
"deleted_at": null,
"latitude": "26.78965",
"longitude": "93.78965",
"is_public": null,
"basedIn": null,
"visit_place": null,
"visit_place_lat": null,
"visit_place_long": null,
"notification_flag": "on",
"location": "abc, xyz, pqr",
"country_code": null,
"home_key": 1,
"postCount": 0,
"userSpotCount": 5,
"itineraryCount": 0
},
{
"id": xxxxx,
"name": "xxxx xxxx",
"email": "xxxx.xxxx#xxxx.xxx",
"facebook_id": null,
"google_id": null,
"apple_id": null,
"is_email_verifie": null,
"is_phone_verifie": null,
"created_at": "2022-06-06T23:38:04.000Z",
"updated_at": "2022-06-06T23:38:04.000Z",
"phone": "1234567890",
"token": null,
"profile_pic": "",
"status": "pending",
"email_token": null,
"deleted_at": null,
"latitude": "26.78965",
"longitude": "93.78965",
"is_public": null,
"basedIn": null,
"visit_place": null,
"visit_place_lat": null,
"visit_place_long": null,
"notification_flag": "on",
"location": "abc, xyz, pqr",
"country_code": null,
"home_key": 1,
"postCount": 0,
"userSpotCount": 1,
"itineraryCount": 0
}
]
I need this output as per the order by ASC/DESC

Two different nodes giving same call to same MYSQL Database giving different responses

I have my first NodeJS API running. I have two API's now running (TEST and PROD).
These files are DIRECT copies of each other and running via App Manager in Cpanel.
Weirdly, one of the calls (Prod) is giving incorrect information on some of the calls... like it is giving 'old' data.
eg. a record was changed for an 'order' and a locationID was added to the database.
I have checked the database and it was added.
The test call:
https://www.myAPI.com.au/TEST/orders/myOrders/1709
Response:
{
"Result": true,
"order": {
"orderID": 1709,
"chemOrderID": 1243,
"firstName": "test",
"lastName": "test",
"phone": "04123456",
"email": null,
"prefContact": 1,
"itemName": "KN95 Box Of 30 ",
"itemQuantity": 1,
"plu": null,
"modelNumber": null,
"colour": null,
"itemDetails": null,
"supplier": "avacare",
"itemSize": null,
"amountPaid": 0,
"receiptNumber": "00",
"quotedPrice": 69.99,
"orderComments": null,
"reorder": 0,
"isNDIS": 0,
"isNDISPaid": 0,
"onHold": 0,
"requestedBy": 35,
"orderedBy": 35,
"receivedBy": 35,
"finalisedBy": 0,
"overdueBy": 0,
"NDISBy": 0,
"deletedBy": 0,
"requestedByName": "Jac",
"orderedByName": "Jac",
"receivedByName": "Jac",
"finalisedByName": null,
"overdueByName": null,
"deletedByName": null,
"requestedDateShort": "18 Jan",
"orderedDateShort": "18 Jan",
"receivedDateShort": "18 Jan",
"finalisedDateShort": null,
"overdueDateShort": null,
"deletedDateShort": null,
"requestedDate": "2022-01-18T09:40:21",
"orderedDate": "2022-01-18T09:50:33",
"receivedDate": "2022-01-18T12:21:41",
"finalisedDate": null,
"overdueDate": null,
"deletedDate": null,
"etaMaxDateShort": "Thu, 20 Jan",
"etaCode": 2,
"locationID": 199,
"supplierRef": null,
"locationOther": null,
"categoryID": 5,
"categoryName": "GENERAL SHOP"
}
}
Now this is correct. The locationID is updated, the receivedDate, recivedByName and receivedBy are all correct and updated.
The same call on copied API: (same mysql user login. literally a duplicate of the api)
Call:
https://www.myAPI.com.au/prod/orders/myOrders/1709
Response:
{
"Result": true,
"order": {
"orderID": 1709,
"chemOrderID": 1243,
"firstName": "test",
"lastName": "test",
"phone": "04123456",
"email": null,
"prefContact": 1,
"itemName": "KN95 Box Of 30 ",
"itemQuantity": 1,
"plu": null,
"modelNumber": null,
"colour": null,
"itemDetails": null,
"supplier": "avacare",
"itemSize": null,
"amountPaid": 0,
"receiptNumber": "00",
"quotedPrice": 69.99,
"orderComments": null,
"reorder": 0,
"isNDIS": 0,
"isNDISPaid": 0,
"onHold": 0,
"requestedBy": 35,
"orderedBy": 35,
"receivedBy": 0,
"finalisedBy": 0,
"overdueBy": 0,
"NDISBy": 0,
"deletedBy": 0,
"requestedByName": "Jac",
"orderedByName": "Jac",
"receivedByName": null,
"finalisedByName": null,
"overdueByName": null,
"deletedByName": null,
"requestedDateShort": "18 Jan",
"orderedDateShort": "18 Jan",
"receivedDateShort": null,
"finalisedDateShort": null,
"overdueDateShort": null,
"deletedDateShort": null,
"requestedDate": "2022-01-18T09:40:21",
"orderedDate": "2022-01-18T09:50:33",
"receivedDate": null,
"finalisedDate": null,
"overdueDate": null,
"deletedDate": null,
"etaMaxDateShort": "Thu, 20 Jan",
"etaCode": 2,
"locationID": null,
"supplierRef": null,
"locationOther": null,
"categoryID": 5,
"categoryName": "GENERAL SHOP"
}
}
As can be seen, the aforementioned fields aren't filled!?
It's like the call is getting an old version of the database or something...
I can't understand this at all.
There is only one DB, i haven't created a test db, in reality this whole thing is testing (even the prod version), as I am learning and trying to understand how I will go about everything once it is actually live.

Get N number of values from object with jq

I'm trying to retrieve a given number of values from the following json object (just used 2 for brevity):
[
{
"content": null,
"deleted": false,
"metadata": null,
"name": "name/windows-2016-osDisk.170b8936-d66d-4139-a409-26f4e9d354fe.vhd",
"properties": {
"appendBlobCommittedBlockCount": null,
"blobTier": "P10",
"blobTierChangeTime": null,
"blobTierInferred": true,
"blobType": "PageBlob",
"contentLength": 136367309312,
"contentRange": null,
"contentSettings": {
"cacheControl": null,
"contentDisposition": null,
"contentEncoding": null,
"contentLanguage": null,
"contentMd5": "-0q970378r08==",
"contentType": "application/octet-stream"
},
"copy": {
"completionTime": null,
"id": null,
"progress": null,
"source": null,
"status": null,
"statusDescription": null
},
"creationTime": "2019-11-21T08:34:33+00:00",
"deletedTime": null,
"etag": "0237502375",
"lastModified": "2019-11-21T08:34:33+00:00",
"lease": {
"duration": null,
"state": "available",
"status": "unlocked"
},
"pageBlobSequenceNumber": null,
"remainingRetentionDays": null,
"sequenceNumber": 1,
"serverEncrypted": true
},
"snapshot": null
},
{
"content": null,
"deleted": false,
"metadata": null,
"name": "name/windows-2019-osDisk.f122fb3c-0edb-42a8-b98c-56657b447f15.vhd",
"properties": {
"appendBlobCommittedBlockCount": null,
"blobTier": "P10",
"blobTierChangeTime": null,
"blobTierInferred": true,
"blobType": "PageBlob",
"contentLength": 136367309312,
"contentRange": null,
"contentSettings": {
"cacheControl": null,
"contentDisposition": null,
"contentEncoding": null,
"contentLanguage": null,
"contentMd5": "0237502375/hKOg==",
"contentType": "application/octet-stream"
},
"copy": {
"completionTime": null,
"id": null,
"progress": null,
"source": null,
"status": null,
"statusDescription": null
},
"creationTime": "2019-11-21T08:35:03+00:00",
"deletedTime": null,
"etag": "20397520i3h523",
"lastModified": "2019-11-21T08:35:03+00:00",
"lease": {
"duration": null,
"state": "available",
"status": "unlocked"
},
"pageBlobSequenceNumber": null,
"remainingRetentionDays": null,
"sequenceNumber": 1,
"serverEncrypted": true
},
"snapshot": null
}
]
I sort this on creationTime in jq like so: jq 'sort_by(.properties.creationTime)' When I pipe this through | .[].name making jq 'sort_by(.properties.creationTime) | .[].name I get a sorted list of the names.
My question is: How can I pass an integer to this command and remove these many names, starting from the most recent created (bottom)?
following
https://stedolan.github.io/jq/manual/v1.6/#Builtinoperatorsandfunctions
slicing should work:
jq 'sort_by(.properties.creationTime) | .[-1:]'
The answer I've used is the following:
jq 'sort_by(.properties.creationTime) | .[-4:] | .[].name'
Where -4 is the amount of names to display starting from the bottom.

How to convert few columns of pandas dataframe into JSON record within another record?

So I am trying to convert a pandas Dataframe with 12+ columns into corresponding JSON Record. I'm able to get it. However, I want few columns of the frame to be a subrecord of a new column. How this can be achieved.?
{
"ADRNR": 2692629,
"AlertID": "",
"AlertTimestamp": "14-12-2015 14:44:14",
"BANKL": null,
"BANKN": null,
"BANKS": "nan",
"BEGRU": "NPIV",
"BUKRS": "2646",
"C_Block": "No",
"KOINH": null,
"LAND1": "US",
"LOEVM_x": null,
"LOEVM_y": null,
"MasterDataID": "10099",
"MasterDataType": "Vendor",
"NAME1": "LEGAL",
"NODEL_x": null,
"NODEL_y": null,
"ORT01": null,
"OtherData": null,
"PSTLZ": null,
"RuleID": "Rule3",
"RuleName": "Vendor and Bank Country is Different",
"STCD1": null,
"STCD2": null,
"STCEG": null,
"STRAS": null,
"TELF1": null
}
The above JSON is what I Get. But I want the following structure. Kindly guide me.
{
"RuleID": "Rule3",
"RuleName": "Vendor and Bank Country is Different",
"AlertID": "",
"AlertTimestamp": "14-12-2015 14:44:14",
"MasterDataID": "10099",
"MasterDataType": "Vendor",
"OtherData": {
"BANKL": null,
"BANKN": null,
"BANKS": "nan",
"BEGRU": "NPIV",
"BUKRS": "2646",
"C_Block": "No",
"KOINH": null,
"LAND1": "US",
"LOEVM_x": null,
"LOEVM_y": null,
"NAME1": "LEGAL",
"NODEL_x": null,
"NODEL_y": null,
"ORT01": null,
"PSTLZ": null,
"ADRNR": 2692629,
"STCD1": null,
"STCD2": null,
"STCEG": null,
"STRAS": null,
"TELF1": null
}
}
Edit : Flg is my code
Final_Table['AlertID'] = ''
Final_Table['AlertTimestamp'] = datetime.now().strftime('%d-%m-%Y %H:%M:%S')
Final_Table['MasterDataType'] = 'Vendor'
Final_Table['RuleID'] = 'Rule3'
Final_Table['RuleName'] = 'Vendor and Bank Country is Different'
Final_Table = Final_Table.rename(columns={'LIFNR': 'MasterDataID'})
Result = Final_Table[Final_Table['BANKS'] != Final_Table['LAND1']]
Result['OtherData'] = np.NaN
final_result = {'alerts': json.loads(Result.to_json(orient = 'records',force_ascii = False).encode('utf8'))}
result = {'results': final_result}
with open('output_Rule3.json', 'w') as outfile:
json.dump(result, outfile, indent = 5, sort_keys = True)
log.info("Rule3 : Execution Successful")
Rather than within pandas, I would modify the json output.
Starting with your json data
print(json.dumps(data, indent=4, sort_keys=True))
[
{
"ADRNR": 2692629,
"AlertID": "",
"AlertTimestamp": "14-12-2015 14:44:14",
"BANKL": null,
"BANKN": null,
"BANKS": "nan",
"BEGRU": "NPIV",
"BUKRS": "2646",
"C_Block": "No",
"KOINH": null,
"LAND1": "US",
"LOEVM_x": null,
"LOEVM_y": null,
"MasterDataID": "10099",
"MasterDataType": "Vendor",
"NAME1": "LEGAL",
"NODEL_x": null,
"NODEL_y": null,
"ORT01": null,
"OtherData": null,
"PSTLZ": null,
"RuleID": "Rule3",
"RuleName": "Vendor and Bank Country is Different",
"STCD1": null,
"STCD2": null,
"STCEG": null,
"STRAS": null,
"TELF1": null
}
]
and a list of some of the columns you are interested in:
other_data = ["BANKL", "BANKN", "BANKS", "BEGRU", "BUKRS", "C_Block"]
you can modify the json directly:
for i, record in enumerate(data):
data[i]['OtherData'] = {}
for key in list(record.keys()):
if key in other_data:
data[i]['OtherData'][key] = record[key]
del data[i][key]
print(json.dumps(data, indent=4, sort_keys=True))
[
{
"ADRNR": 2692629,
"AlertID": "",
"AlertTimestamp": "14-12-2015 14:44:14",
"KOINH": null,
"LAND1": "US",
"LOEVM_x": null,
"LOEVM_y": null,
"MasterDataID": "10099",
"MasterDataType": "Vendor",
"NAME1": "LEGAL",
"NODEL_x": null,
"NODEL_y": null,
"ORT01": null,
"OtherData": {
"BANKL": null,
"BANKN": null,
"BANKS": "nan",
"BEGRU": "NPIV",
"BUKRS": "2646",
"C_Block": "No"
},
"PSTLZ": null,
"RuleID": "Rule3",
"RuleName": "Vendor and Bank Country is Different",
"STCD1": null,
"STCD2": null,
"STCEG": null,
"STRAS": null,
"TELF1": null
}
]