need struct type but got string: Cannot extract value - json

I have a tricky scenario that I need help with, I have to extract information from a field.
The Data set can EITHER look like this:
"orderDelivery": {
"id":123,
"deliveryFee": 3500,
"deliveryFeeExcludingVAT": 3500,
"deliveryFeeRefunded": true,
"immediatedeliveryselected": true,
"deliverySlotId": null,
"deliverySlotChanged": false,
"driver": null,
"storeCollectionSLATime": 1635759479391,
"storeArrivalTimeEstimate": null,
"storeArrivalTimeActual": null,
"timeDelivered": null,
"timeCollected": null,
"DeliveryEstimates": [
{
"estimatedDeliveryTime": 1635762179390,
"createdOn": 1635758579390
}
],
"instructions": null,
"deliveryInstructions": null,
"deliveryPartnerCommunication": {
"informedOfNewOrder": true,
"serviceMessage": null
},
"deliveryIssueIds": null
}
Or it can look like this:
"orderDelivery": {
"id":1234,
"deliveryFee": 3500,
"deliveryFeeExcludingVAT": 3500,
"deliveryFeeRefunded": true,
"immediatedeliveryselected": true,
"deliverySlotId": null,
"deliverySlotChanged": false,
"driver": null,
"storeCollectionSLATime": 1635759479391,
"storeArrivalTimeEstimate": null,
"storeArrivalTimeActual": null,
"timeDelivered": null,
"timeCollected": null,
"DeliveryEstimates": [
{
"estimatedDeliveryTime": 1635762179390,
"createdOn": 1635758579390
}
],
"instructions": null,
"deliveryInstructions": [
{
"createdOn": 1635762179390,
"deliveryMessage": "delivered"
},
{
"createdOn": 1735762179390,
"deliveryMessage": "not door"
}
],
"deliveryPartnerCommunication": {
"informedOfNewOrder": true,
"serviceMessage": null
},
"deliveryIssueIds": null
}
I need to take into account when deliveryInstructions is null or when it is an array.
I am extracting the data like this:
df["orderDelivery"]["deliveryInstructions"]["createdOn"].cast(StringType()).alias("created_on")
And I am also extracting like this :
df["orderDelivery"]["deliveryInstructions"]["deliveryMessage"].cast(StringType()).alias("delivery_message")
When I query it when is null, I get the following error:
Can't extract value from orderDelivery#27.deliveryInstructions: need struct type but got
string
Can anyone help on how to handle this?
also when when delivery instruction repeats I want it look like this
id
createdOn
deliveryMessage
1234
1635762179390
delivered
1234
1735762179390
Not door
There can be one or more delivery instructions in addition to the null value.
If it's null then I just want null for both CreatedOn and DeliveryMessage

Related

want to get approval status for files

I'm trying to get the status, type, and set that a document is in. I'm making the call to the data/v1/projects/{projectId}/items/{itemId}/versions endpoint
While looking at the UI I noticed that one of the endpoints the UI uses
https://developer.api.autodesk.com/dm/v1/projects/801f898a-4cc9-42cd-a56b-4e4b9ca0a054/folders/urn%3Aadsk.wipemea%3Afs.folder%3Aco.mgQJS4w7SUOx5EbM4gVJBA/documents?entity_types=SEED_FILE&limit=200&offset=0
contain some of the info I need
{
"urn": "urn:adsk.wipemea:dm.lineage:sdpW01kQRVyGOhKFIvkELA",
"create_time": "2021-02-01T08:42:06+0000",
"create_user_id": "SE6ZLLDDEY9R",
"create_user_name": "MICHAEL LEIPPER",
"last_modified_time": "2021-02-01T08:42:07+0000",
"last_modified_user_id": "SE6ZLLDDEY9R",
"last_modified_user_name": "MICHAEL LEIPPER",
"delete_by": null,
"delete_at": null,
"title_block_id": null,
"title_block_image": null,
"file_name": "happy-familyNHS (1).jpg",
"id": "b1da56d3-5910-455c-863a-128522f9042c",
"name": "happy-familyNHS (1).jpg",
"description": "",
"hidden": false,
"move_state": null,
"cde_is_master": null,
"custom_attributes": [],
"current_version": {
"create_time": "2021-02-01T08:42:06+0000",
"update_time": "2021-02-09T10:42:15+0000",
"created_by": "MICHAEL LEIPPER",
"updated_by": "MICHAEL LEIPPER",
"create_user_name": "MICHAEL LEIPPER",
"create_user_id": "SE6ZLLDDEY9R",
"update_user_id": "SE6ZLLDDEY9R",
"storage_urn": "urn:adsk.objects:os.object:wip.dm.emea.2/4b48ee2d-78fd-4e17-876c-48400c9cf6b6.jpg",
"urn": "urn:adsk.wipemea:fs.file:vf.sdpW01kQRVyGOhKFIvkELA?version=1",
"states": [
"CONTENT_AVAILABLE"
],
"name": "happy-familyNHS (1).jpg",
"title": "happy-familyNHS (1).jpg",
"mime_type": null,
"revision_number": 1,
"title_block_id": null,
"original_name": null,
"file_name": "happy-familyNHS (1).jpg",
"custom_attributes": [],
"sets": [],
"approve_status": {
"id": "2715ab6c-46b8-4493-b48a-426056e93cae",
"value": "approved",
"label": "Approved As built",
"iconValue": "approved-check",
"buildIn": false,
"approverUserId": "SE6ZLLDDEY9R",
"approverUserName": "MICHAEL LEIPPER"
},
"translation_states": "none",
"translation_has_thumbnail": false,
"forge_type": null,
"forged_is_composite_design": null,
"entity_type": "SEED_FILE",
"bubble_viewable_guid": null,
"bubble_viewable_id": null,
"bubble_viewable_order": null,
"bubble_viewable_resource_mimetype": null,
"bubble_viewable_resource_urn": null,
"bubble_urn": "urn:adsk.wipemea:fs.file:vf.sdpW01kQRVyGOhKFIvkELA?version=1",
"process_state": "PROCESSING_COMPLETE",
"extraction_state": "SUCCESS",
"splitting_state": "NOT_SPLIT",
"review_id": null,
"review_state": "NOT_IN_REVIEW",
"review_new_document_count": null,
"review_matched_document_count": null,
"review_deleted": null,
"action": null,
"partial_extraction_failure": null,
"move_state": null,
"dm_command_id": "ad123f4d-04df-4620-b733-316d346856cf",
"id": "6986e92e-7a83-4769-9101-e8a5615568c8",
"file_size": 136688,
"process_result": "PROCESSING_SUCCESS"
},
"current_set_version": 0,
"reserved": false,
"reserved_user_name": null,
"reserved_user_id": null,
"reserved_at": null,
"latest_version": 1,
"latest_version_file_state": [
"CONTENT_AVAILABLE"
],
"latest_version_create_time": "2021-02-01T08:42:06+0000",
"current_version_urn": "urn:adsk.wipemea:fs.file:vf.sdpW01kQRVyGOhKFIvkELA?version=1",
"latest_storage_urn": "urn:adsk.objects:os.object:wip.dm.emea.2/4b48ee2d-78fd-4e17-876c-48400c9cf6b6.jpg",
"parent_folder_urn": "urn:adsk.wipemea:fs.folder:co.mgQJS4w7SUOx5EbM4gVJBA",
"origin_folder_urn": null,
"folder_set_urn": null,
"latest_versioned_file_urn": "urn:adsk.wipemea:fs.file:vf.sdpW01kQRVyGOhKFIvkELA?version=1"
},
As can be seen this has approve_status in the return object. I know that this endpoint is private and not available publicly, however, is there a way to get these approval statuses for each file.
Any help is greatly appreciated.
Sounds like you are looking for BIM 360 Docs Review and Set API. Unfortunately, there is no public API and they are still under wish:
ALEX-37150: “API wish: expose Docs Review API”
ALEX-24690: “Expose BIM 360 SET via DM API”
Sorry for the bad news.
I will add your name to the wish log above. In this context, you are interested in read access to those?

Receiving c# dictionary serialise in JSON, and need to deserialise it in my angular 6 (typscript) website

I get a JSON string from an API, and I want to deserialise it in my Angular 6 website.
I don't know how dictionaries work in TypeScript. So I try to avoid them as much as I can when it should be received from an api.
When I do :
console.log(JSON.parse(viewed_availab));
I can see this result :
[{
"2019-05-10T00:00:00": {
"TotalStopsCount": null,
"TotalRoutesCount": null,
"TotalStopsCountAfterBooking": null,
"OccupancyRate": null,
"OccupancyRateAfterBokking": null,
"ZoneId": null,
"DomainName": "MTH",
"Date": "2019-05-10T00:00:00",
"IsAvailable": false,
"Status": {
"Message": "Not enough time for items to reach final destination",
"StatusID": 1
},
"IdDomains": null,
"Items": []
},
"2019-05-10T00:00:00": {
"TotalStopsCount": null,
"TotalRoutesCount": null,
"TotalStopsCountAfterBooking": null,
"OccupancyRate": null,
"OccupancyRateAfterBokking": null,
"ZoneId": null,
"DomainName": "MTH",
"Date": "2019-05-10T00:00:00",
"IsAvailable": false,
"Status": {
"Message": "Not enough time for items to reach final destination",
"StatusID": 1
},
"IdDomains": null,
"Items": []
}]
And here is the request I execute to receive this string:
this.dataService.post('cleard.api.timeslot.viewed_availabilities', new ViewedAvailabilityParams(search.postalCode, search.userName, search.date, search.businessUnitId, search.originLocationId))
.then((viewed_availab: string) => {
...
}).catch((error) => { this.searching = false; });
I would like the result to be in a typescript dictionary :
Dictionary<Date, Availibility> // in a typescript way.
where date represents the key (first line of the Json), and availability represents the content:
{"TotalStopsCount": null,
"TotalRoutesCount": null,
"TotalStopsCountAfterBooking": null,
"OccupancyRate": null,
... and so on }
(I already have a model done for Availability).
A dictionary in Typescript/Javascript is just an Object. You can iterate through it using Object.entries:
const dictionary = {
'key1': value1,
'key2': value2
};
Object.entries(dictionary).forEach(([key, value]) => {
// ...
});
Appropriate types that might be appropriate: Record<TKey, TValue>, { [key: string]: T }

How can I extract response from json array in Karate

I am trying to extract response[0].otpValue but it does not work
I have tried to:
get[0] response.id
response[0].otpValue
{
"errorCode": null,
"response": [
{
"createdTimestamp": 1557314537720,
"lastUpdatedTimestamp": 1557314537720,
"createdBy": null,
"lastUpdatedBy": null,
"otpValue": "5346",
"expiryTimestamp": 1556807308797,
"validNow": false,
"sessionUserId": null
}
],
"status": "SUCCESS",
"errorMessage": null
}
I am looking to get otpValue from above json response
* def firstOtp = response[0].id
* def answer = { "errorCode": null, "response": [ { "createdTimestamp": 1557314537720, "lastUpdatedTimestamp": 1557314537720, "createdBy": null, "lastUpdatedBy": null, "otpValue": "5346", "expiryTimestamp": 1556807308797, "validNow": false, "sessionUserId": null } ], "status": "SUCCESS", "errorMessage": null }
* def firstOtp = answer.response[0].otpValue
* match firstOtp == "5346"

Not able to Access the JSON getting error

I am not able to access json. PFB json
I am trying to access:-
this.businessSwitchName = d.businessSwitchDetails.nodeName;
and trying to bind to html via interpolation
var d:any = {
"service": "091PUNE623017759708",
"routerDetails": {
"nodeName": null,
"portName": null,
"nodeIP": null,
"nodeID": null
},
"agsDetails": {
"nodeName": null,
"portName": null,
"nodeIP": null,
"nodeID": null
},
"businessSwitchDetails": {
"nodeName": "Dyaneshwar Park",
"portName": null,
"nodeIP": "10.171.4.3",
"nodeID": "365429"
},
"handoffPort": null,
"qosLoopingPort": null
}
But getting this error:
ERROR TypeError: Cannot read property 'businessSwitchDetails' of undefined
at ContactFormComponent.push../src/app/contact-form/contact-form.component.ts.ContactFormComponent.onChange (contact-form.component.ts:16)
at Object.eval (ContactFormComponent.html:48)
at handleEvent (core.js:9953)
at callWithDebugContext (core.js:11046)
at Object.debugHandleEvent [as handleEvent] (core.js:10749)
at dispatchEvent (core.js:7415)
at core.js:7859
at HTMLInputElement.<anonymous> (platform-browser.js:1140)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:3662)
I can not comment so need to do this in a answer, but you need to show some more information.
Where do you initialize the variable d, because it seems like d is not in the same scope as businessSwitchName
You initialize the variable d after trying to use it.
Which means while you try to do:
this.businessSwitchName = d.businessSwitchDetails.nodeName;
Variable d is still undefined, so you should first define d and than do something with it.
So make your onChange() method something like this:
onChange(){
var d = {"service": "091PUNE623017759708",
"routerDetails": {
"nodeName": null,
"portName": null,
"nodeIP": null,
"nodeID": null
},
"agsDetails": {
"nodeName": null,
"portName": null,
"nodeIP": null,
"nodeID": null
},
"businessSwitchDetails": {
"nodeName": "Dyaneshwar Park",
"portName": null,
"nodeIP": "10.171.4.3",
"nodeID": "365429"
},
"handoffPort": null,
"qosLoopingPort": null};
this.businessSwitchName = d.businessSwitchDetails.nodeName;
this.businessSwitchIp = d.businessSwitchDetails.nodeIP;
this.portName = d.businessSwitchDetails.portName;
let da = {
"serviceAttribute": {
"serviceType": "GVPN",
"category": "category",
"name": "091PUNE623017759708"
},
"btsIp": "10.171.4.3"
};
console.log(da);
this.service.portService(da).
subscribe (response => {
console.log(response);
});
}
}
TypeError: Cannot read property 'businessSwitchDetails' of undefined
As above statement says itself that we are tyring to access businessSwitchDetails property of undefined, states that object d is not defined.
Ways to debug :
Do console.log(d); before this line of code this.businessSwitchName = d.businessSwitchDetails.nodeName; to check whether you are getting proper response or not.
Check the value of d. It should be JSON object instead of JSON string.
Check the sequence of the execution of the statements. It should be object d initialization first then statement to access the property businessSwitchDetails.

jq, search for a 'sections' basing on a value of an attribute

I am new in this stuff,
I have input json:
{
"2017-09-15": {
"key": "stops",
"node": {
"lastUpdate": "2017-09-15 05:15:26",
"stops": [
{
"stopId": 11000,
"stopCode": null,
"stopName": null,
"stopShortName": "1000",
"stopDesc": "Tom Tail",
"subName": "1000",
"zoneId": null,
"zoneName": null,
"stopUrl": "",
"locationType": null,
"parentStation": null,
"stopTimezone": "",
"wheelchairBoarding": null,
"virtual": null,
"nonpassenger": null,
"depot": null,
"ticketZoneBorder": null,
"onDemand": null,
"activationDate": "2017-09-14"
},
{
"stopId": 1100,
"stopCode": "04",
"stopName": "One Three",
"stopShortName": "1100",
"stopDesc": "Big Wall",
"subName": "04",
"zoneId": 1,
"zoneName": "Demo2",
"stopUrl": "",
"locationType": null,
"parentStation": null,
"stopTimezone": "",
"wheelchairBoarding": null,
"virtual": 0,
"nonpassenger": 0,
"depot": 0,
"ticketZoneBorder": 0,
"onDemand": 0,
"activationDate": "2017-09-24"
}
]
}
}
}
I can get all stops by filter:
jq -r '.[].node.stops' json
but how to search for a specific stop, for example basing on stopDescr - exact name or containing a string?
thx.
You're on the right track ...
.[].node.stops[] | select(.stopDesc == "Big Wall")
unique
Regarding the supplementary question:
1) sort and unique both expect arrays as input
2) calling unique obviates the need to call sort
3) unique produces an array, so you might want to use unique[]
4) One way to convert a stream of (JSON) values to an array is to wrap the expression producing the stream in square brackets.
Here is an example:
[.[].node.stops[] | select(.stopDesc == "Big Wall") | .zoneName]
| unique