Parsing API Response in Angular - node.js

all - hoping you can help me learn how to parse a JSON response.
I have a service that makes an API call like this:
private REST_API_SERVER = 'myapicall.com'
constructor(private httpClient: HttpClient) { }
public sendGetRequest(): Observable<any> {
return this.httpClient.get(this.REST_API_SERVER);
}
The above is returned to a component:
sendGetRequest(): void {
this.dataService.sendGetRequest()
.subscribe(data => this.data = data);
}
I can output the response in my component HTML like this:
{{data | json}}
At which point a response with a structure like this is written to the screen:
{
"findItemsByKeywordsResponse": [
{
"ack": [
"Success"
],
"version": [
"1.13.0"
],
"timestamp": [
"2022-03-03T16:12:06.220Z"
],
"searchResult": [
{
"#count": "1",
"item": [
{
"itemId": [
"294786675398"
],
"title": [
"Apple iPhone 12 mini (5G) Factory Unlocked GSM+CDMA 256GB | 128GB | 64GB"
],
"globalId": [
"EBAY-US"
],
"subtitle": [
"✤Cheapest Online ✤FREE SHIPPING ✤FREE RETURN ✤US SELLER"
],
"primaryCategory": [
{
"categoryId": [
"9355"
],
"categoryName": [
"Cell Phones & Smartphones"
]
}
],
"galleryURL": [
"https://i.ebayimg.com/thumbs/images/g/hBYAAOSw~qhgy8mG/s-l140.jpg"
],
"viewItemURL": [
"https://www.ebay.com/itm/Apple-iPhone-12-mini-5G-Factory-Unlocked-GSM-CDMA-256GB-128GB-64GB-/294786675398?var=593157879172"
],
"autoPay": [
"true"
],
"postalCode": [
"112**"
],
"location": [
"Brooklyn,NY,USA"
],
"country": [
"US"
],
"shippingInfo": [
{
"shippingServiceCost": [
{
"#currencyId": "USD",
"__value__": "0.0"
}
],
"shippingType": [
"Free"
],
"shipToLocations": [
"Worldwide"
],
"expeditedShipping": [
"true"
],
"oneDayShippingAvailable": [
"true"
],
"handlingTime": [
"1"
]
}
],
"sellingStatus": [
{
"currentPrice": [
{
"#currencyId": "USD",
"__value__": "439.88"
}
],
"convertedCurrentPrice": [
{
"#currencyId": "USD",
"__value__": "439.88"
}
],
"sellingState": [
"Active"
],
"timeLeft": [
"P28DT9H52M53S"
]
}
],
"listingInfo": [
{
"bestOfferEnabled": [
"false"
],
"buyItNowAvailable": [
"false"
],
"startTime": [
"2022-02-01T02:05:58.000Z"
],
"endTime": [
"2022-04-01T02:04:59.000Z"
],
"listingType": [
"FixedPrice"
],
"gift": [
"false"
],
"watchCount": [
"26"
]
}
],
"returnsAccepted": [
"true"
],
"condition": [
{
"conditionId": [
"1500"
],
"conditionDisplayName": [
"Open box"
]
}
],
"isMultiVariationListing": [
"true"
],
"topRatedListing": [
"true"
]
}
]
}
],
"paginationOutput": [
{
"pageNumber": [
"1"
],
"entriesPerPage": [
"1"
],
"totalPages": [
"12825623"
],
"totalEntries": [
"12825623"
]
}
],
"itemSearchURL": [
"https://www.ebay.com/sch/i.html?_nkw=iPhone&_ddo=1&_ipg=1&_pgn=1"
]
}
]
}
In my component HTML, how would I write this item's title to the screen?

Assuming that
const data={complete_json_you_have_provided}
//then
console.log(data.findItemsByKeywordsResponse[0].searchResult[0].item[0].title);
prints required title array [Apple iPhone 12 mini (5G) Factory Unlocked GSM+CDMA 256GB | 128GB | 64GB]
check here TS PLAYGROUND

I don't understand why (please help educate me if you know), but I got it working by changing the sendGetRequest() function to:
public sendGetRequest(): Observable<any> {
return this.http.get(this.REST_API_SERVER);
}
Rather than:
public sendGetRequest(): Observable<any> {
return this.httpClient.get(this.REST_API_SERVER);
}

Related

How to convert LabelMe output to COCO format?

I am new to this, can any one please help me to convert labelme json output to coco format output.
here is labelme output file. Thanks in advance.
{
"version": "5.1.1",
"flags": {},
"shapes": [
{
"label": "bean",
"points": [
[
3183.5227272727275,
459.65909090909093
],
[
3174.431818181818,
468.1818181818182
],
[
3162.5,
476.1363636363636
],
[
3151.1363636363635,
487.5
],
[
3144.318181818182,
508.52272727272725
],
[
3142.0454545454545,
543.75
],
[
3144.8863636363635,
571.0227272727273
],
[
3151.7045454545455,
589.7727272727273
],
[
3159.090909090909,
602.8409090909091
],
[
3168.181818181818,
614.2045454545455
],
[
3178.9772727272725,
621.5909090909091
],
[
3192.6136363636365,
628.4090909090909
],
[
3208.5227272727275,
628.4090909090909
],
[
3231.25,
619.8863636363636
],
[
3256.25,
604.5454545454545
],
[
3271.590909090909,
583.5227272727273
],
[
3277.2727272727275,
553.9772727272727
],
[
3277.2727272727275,
524.4318181818181
],
[
3271.0227272727275,
496.59090909090907
],
[
3257.3863636363635,
471.59090909090907
],
[
3238.068181818182,
459.09090909090907
],
[
3212.5,
455.6818181818182
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
},
{
"label": "bean",
"points": [
[
2968.75,
320.45454545454544
],
[
2952.840909090909,
327.27272727272725
],
[
2935.7954545454545,
333.52272727272725
],
[
2922.7272727272725,
341.47727272727275
],
[
2915.340909090909,
358.52272727272725
],
[
2911.931818181818,
371.59090909090907
],
[
2913.068181818182,
388.6363636363636
],
[
2919.318181818182,
405.6818181818182
],
[
2928.409090909091,
423.8636363636364
],
[
2947.7272727272725,
448.8636363636364
],
[
2967.6136363636365,
460.22727272727275
],
[
2986.3636363636365,
463.6363636363636
],
[
3003.409090909091,
464.77272727272725
],
[
3020.4545454545455,
463.6363636363636
],
[
3036.931818181818,
454.54545454545456
],
[
3049.431818181818,
446.02272727272725
],
[
3056.818181818182,
431.8181818181818
],
[
3063.068181818182,
411.9318181818182
],
[
3065.340909090909,
390.34090909090907
],
[
3056.818181818182,
372.15909090909093
],
[
3044.318181818182,
353.40909090909093
],
[
3026.7045454545455,
338.6363636363636
],
[
3013.6363636363635,
328.40909090909093
],
[
2992.6136363636365,
318.1818181818182
],
[
2982.9545454545455,
318.75
]
],
"group_id": null,
"shape_type": "polygon",
"flags": {}
},

Not able to fetch reports data : Getting "Could not deserialize ReportingRequest" error

I am trying to get reports data from spotify ads api using python:
reports = requests.post('https://api-partner.spotify.com/ads/v1.3/adAccounts/8a578aa1-e36d-42da-8110-52914f99f907/report',headers=headers,data=data)
I keep on getting this error :
{"error":{"type":"ParsingError","message":"Request is invalid","errors":["Could not deserialize ReportingRequest"],"sp_trace_id":"89e3b470-6bdd-4e03-8903-d4e814e71164"}}
The header part is correct as it working with other api endpoints like 'get campaign data', but I am suspecting the error is because of the 'data' part which is given by:
data = {
"filters": {
"campaign_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"ad_set_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"ad_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"advertiser_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"country_isos": [
"string"
],
"region_ids": [
0
],
"dma_ids": [
0
],
"platforms": "IOS",
"campaign_statuses": "ACTIVE",
"ad_set_statuses": "ACTIVE",
"ad_set_start_dates": [
"2019-08-24T14:15:22Z"
],
"ad_set_end_dates": [
"2019-08-24T14:15:22Z"
],
"cost_type_filters": "CPCL",
"report_datetime_range": {
"report_start": "2019-08-24T14:15:22Z",
"report_end": "2019-08-24T14:15:22Z"
}
},
"dimensions": "CAMPAIGN",
"fields": "CLICKS",
"organization_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"ad_account_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"continuation_token": "string",
"page_size": 0
}
I tried using the curl command instead of python but still get the same error in the terminal :
curl --request POST \
--url https://api-partner.spotify.com/ads/v1.3/report \
--header 'Authorization: ' \
--header 'Content-Type: application/json' \
--data '{
"filters": {
"campaign_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"ad_set_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"ad_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"advertiser_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"country_isos": [
"string"
],
"region_ids": [
0
],
"dma_ids": [
0
],
"platforms": "IOS",
"campaign_statuses": "ACTIVE",
"ad_set_statuses": "ACTIVE",
"ad_set_start_dates": [
"2019-08-24T14:15:22Z"
],
"ad_set_end_dates": [
"2019-08-24T14:15:22Z"
],
"cost_type_filters": "CPCL",
"report_datetime_range": {
"report_start": "2019-08-24T14:15:22Z",
"report_end": "2019-08-24T14:15:22Z"
}
},
"dimensions": "CAMPAIGN",
"fields": "CLICKS",
"organization_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"ad_account_ids": [
"497f6eca-6276-4993-bfeb-53cbbbba6f08"
],
"continuation_token": "string",
"page_size": 0
}'
I was able to get the response when I passed the data in the following form:
data = {
"dimensions": [
"COST_TYPE"
],
"fields": [
"CLICKS",
"COMPLETES",
"COMPLETION_RATE",
"CONVERSION_RATE",
"CTR",
"ECPCL",
"ECPM",
"ERRORS",
"FETCHES",
"FETCH_REACH",
"FIRST_QUARTILES",
"IMPRESSIONS",
"INTENT_RATE",
"LISTENERS",
"MIDPOINTS",
"NEW_LISTENERS",
"NEW_LISTENER_CONVERSION_RATE",
"NEW_LISTENER_STREAMS",
"OFF_SPOTIFY_IMPRESSIONS",
"PAID_LISTENS",
"PAID_LISTEN_FREQ",
"PAID_LISTEN_REACH",
"RENDER_RATIO",
"SERVES",
"SERVE_FREQ",
"SERVE_REACH",
"SKIPS",
"SPEND",
"STARTS",
"STREAMS",
"STREAMS_PER_NEW_LISTENER",
"STREAMS_PER_USER",
"THIRD_QUARTILES"
],
"filters": {
"ad_ids": [
"5e30eed4-a4fe-4f8e-854c-33b438ab11b6"
],
"report_datetime_range": {
"report_start": "2022-07-13T00:00:00Z",
"report_end": "2022-07-16T23:00:00Z"
}
},
}
The issue seemed to be in the report_start and report end dates.
According to the documentation, the minutes and seconds have to be passed as 00:00 as shown above

how to get all details of a particular userid in a json file in python

i want to access the particular userid details
[
{
"userID": 998926445,
"contentID": [
[
"5bbae768c1df412352000004"
],
[
"5ba8d4fac1df413dae0002cf"
],
[
"5ca61afced8f7d3a5f00102d"
],
[
"5b9c9cacc1df41453400003f"
],
[
"5c8a8044a58c4046b30030f2"
],
[
"5ba9070bc1df413dae0003c3"
],
[
"5bbb1087c1df4140a6000162"
],
[
"5c95142bed8f7d5ede004ef4"
],
[
"5ba905e5c1df413dae0003b9"
],
[
"5bb89799c1df41262300062a"
]
]
},
{
"userID": 998926445,
"contentID": [
[
"5baa8ef5c1df41479a0004b8"
],
[
"5c8a8063a58c4046c8000e89"
],
[
"5bbc7a16c1df412a82000008"
],
[
"5bb8964ec1df41262300060c"
],
[
"5bbc4f92c1df4140a6000abe"
],
[
"5bbb0ecbc1df4140a60000fc"
],
[
"5ba90aa2c1df413dae000429"
],
[
"5bf2bb06c1df411238003054"
],
[
"5cb0c006ed8f7d6a1d00146a"
],
[
"5bbc9825c1df41384100024c"
]
]
},
{
"userID": 998926445,
"contentID": [
[
"5bb8974cc1df412623000622"
],
[
"5b9c9cadc1df414534000047"
],
[
"5b8e5b32c1df412918000048"
],
[
"5b9c9cacc1df41453400003f"
],
[
"5bb8ac8ac1df4126230008a0"
],
[
"5b9fad7bc1df4145340000a7"
],
[
"5bbb1171c1df4140a600016c"
],
[
"5c8a8071a58c4046c8000e8d"
],
[
"5ba90dbac1df413dae00043d"
],
[
"5ba8f905c1df413dae000397"
]
]
}
Try to do something like this. You will get the list. then you can work on fixing it. Let us assume json_list is your original json list.
from collections import defaultdict
dd = defaultdict(list)
for i in json_list:
dd[i['userID']].append([j[0] for j in i['contentID']])
dd = dict(dd)
print(dd)
Your output will be something like this:
{998926445: [['5bbae768c1df412352000004', '5ba8d4fac1df413dae0002cf', '5ca61afced8f7d3a5f00102d', '5b9c9cacc1df41453400003f', '5c8a8044a58c4046b30030f2', '5ba9070bc1df413dae0003c3', '5bbb1087c1df4140a6000162', '5c95142bed8f7d5ede004ef4', '5ba905e5c1df413dae0003b9', '5bb89799c1df41262300062a'], ['5baa8ef5c1df41479a0004b8', '5c8a8063a58c4046c8000e89', '5bbc7a16c1df412a82000008', '5bb8964ec1df41262300060c', '5bbc4f92c1df4140a6000abe', '5bbb0ecbc1df4140a60000fc', '5ba90aa2c1df413dae000429', '5bf2bb06c1df411238003054', '5cb0c006ed8f7d6a1d00146a', '5bbc9825c1df41384100024c'], ['5bb8974cc1df412623000622', '5b9c9cadc1df414534000047', '5b8e5b32c1df412918000048', '5b9c9cacc1df41453400003f', '5bb8ac8ac1df4126230008a0', '5b9fad7bc1df4145340000a7', '5bbb1171c1df4140a600016c', '5c8a8071a58c4046c8000e8d', '5ba90dbac1df413dae00043d', '5ba8f905c1df413dae000397']]}

Parse result of XML -> XML2JS ->JSON.stringify using JQ

I have a file created by doing an XHR fetch of XML and parsing it through the node module xlm2js and then JSON.stringify. It has about 700 segments of two basic types. This is an edited version of the file with one segment of each type:
{
"NewDataSet": {
"Table": [
{
"SegmentID": [
"2342"
],
"StationID": [
"005es00045:_MN_Stn"
],
"SegmentName": [
"I-5 NB MP0.45 # SR-14"
],
"SegmentType": [
"2"
],
"SegmentLength": [
"1135"
],
"MinimumLanesReporting": [
"0.5"
],
"CalculationThreshold": [
"30"
],
"CalculationPeriod": [
"2"
],
"MinimumSamples": [
"3"
],
"SegmentMaximumFilter": [
"774"
],
"SegmentMinimumFilter": [
"12"
],
"StandardDeviationSamples": [
"15"
],
"StandardDeviationMultiplier": [
"1.96"
],
"UseStandardDeviationFilter": [
"false"
],
"IsActive": [
"true"
]
},
{
"SegmentID": [
"3051"
],
"BeginningDcuID": [
"584"
],
"EndDcuID": [
"589"
],
"SourceSystem": [
"TravelTime"
],
"SegmentName": [
"OR212 at SE 242nd Ave to OR212 at SE Foster Rd"
],
"SegmentType": [
"1"
],
"SegmentLength": [
"100"
],
"CalculationThreshold": [
"60"
],
"CalculationPeriod": [
"10"
],
"MinimumSamples": [
"3"
],
"SegmentMaximumFilter": [
"3600"
],
"SegmentMinimumFilter": [
"50"
],
"StandardDeviationSamples": [
"20"
],
"StandardDeviationMultiplier": [
"1.96"
],
"UseStandardDeviationFilter": [
"true"
],
"IsActive": [
"true"
]
}
]
}
}
I need to ignore the "SegmentType":["2"] segments and extract SegmentID, SegmentName, BeginningDcuID, EndingDcuID, and SegmentLength from the type 1 segments where IsActive is true.
I can list the file with jq "." but any attempt at other operations with jq fail, usually with the message:
'jq: error: syntax error, unexpected '[' (Unix shell quoting issues?) at , line 1:'
Any suggestions for jq syntax changes or xml2js parameter changes to make this work would be outstandingly helpful.
Never use double quotes for quoting an argument if there is nothing in it that you want the shell to expand.
$ jq '.NewDataSet.Table[]
| select(.SegmentType[0] != "2" and .IsActive[0] == "true")
| (.SegmentID, .SegmentName, .BeginningDcuID, .EndingDcuID, .SegmentLength)[0]' file
"3051"
"OR212 at SE 242nd Ave to OR212 at SE Foster Rd"
"584"
null
"100"

MultiPolgyon search on MongoDB

I can't seem to figure this out and would appreciate any and all help.
I am using Node.js to make a query to MongoDB using the MultiPolygon filter. I am connecting to the native driver rather than using Mongoose because it doesn't seem like Mongoose supports MultiPolygon (absent in docs).
I receive a malformed query error though
GatheredListings.collection.find({
$and: [
{ 'rentAmount': { $gte: filterParameters.minPrice } },
{ 'rentAmount': { $lte: filterParameters.maxPrice } },
{ 'availabilityDate': { $gte: filterParameters.date } },
{ 'propertyType': filterParameters.propertyType },
{ 'bedrooms': filterParameters.bedrooms },
{ 'location': {
$geoWithin: {
$geometry: {
type : "MultiPolygon" ,
coordinates: [
geoArrayCollection
]
}
}}
}
]
}).toArray(function(err, properties) {
if (err || !properties)
{
console.log('err ' + err);
callback("No properties found", null);
}
else {
console.log('Number of properties retrieved: ' + properties.length);
callback(null, properties);
}
});`
The error is :
`err MongoError: Malformed geo query: { $geoWithin: { $geometry: { type: "MultiPolygon", coordinates: [ [ [ [ 43.77307711737606, -79.53517913818359 ], [ 43.79191518340848, -79.4476318359375 ], [ 43.75559702541283, -79.43887710571289 ], [ 43.7501411993079, -79.46514129638672 ], [ 43.7541091221655, -79.46943283081055 ], [ 43.75547303488856, -79.47406768798828 ], [ 43.75510106177428, -79.47715759277344 ], [ 43.75435710860915, -79.48093414306641 ], [ 43.75200119590339, -79.48282241821289 ], [ 43.74592499302, -79.48316574096678 ], [ 43.74282465186857, -79.49621200561523 ], [ 43.7404682852067, -79.49604034423828 ], [ 43.73873195568971, -79.49295043945312 ], [ 43.73525914559611, -79.49501037597655 ], [ 43.73538317799622, -79.50410842895508 ], [ 43.73290248118248, -79.51148986816406 ], [ 43.73947610307701, -79.51320648193359 ], [ 43.73649945803657, -79.52642440795898 ], [ 43.77307711737606, -79.53517913818359 ] ], [ [ 43.79203909839882, -79.4476318359375 ], [ 43.80380985089954, -79.39647674560547 ], [ 43.76334592336985, -79.38712120056152 ], [ 43.76179622406369, -79.39501762390137 ], [ 43.73600333614323, -79.43381309509277 ], [ 43.79203909839882, -79.4476318359375 ] ] ] ] } } }
Thank you
The MultiPolygon wasn't formatted correctly. You wanted this:
{
"type": "MultiPolygon",
"coordinates": [
[
[
[
43.77307711737606,
-79.5351791381836
],
[
43.79191518340848,
-79.4476318359375
],
[
43.75559702541283,
-79.43887710571289
],
[
43.7501411993079,
-79.46514129638672
],
[
43.7541091221655,
-79.46943283081055
],
[
43.75547303488856,
-79.47406768798828
],
[
43.75510106177428,
-79.47715759277344
],
[
43.75435710860915,
-79.4809341430664
],
[
43.75200119590339,
-79.48282241821289
],
[
43.74592499302,
-79.48316574096678
],
[
43.74282465186857,
-79.49621200561523
],
[
43.7404682852067,
-79.49604034423828
],
[
43.73873195568971,
-79.49295043945312
],
[
43.73525914559611,
-79.49501037597655
],
[
43.73538317799622,
-79.50410842895508
],
[
43.73290248118248,
-79.51148986816406
],
[
43.73947610307701,
-79.5132064819336
],
[
43.73649945803657,
-79.52642440795898
],
[
43.77307711737606,
-79.5351791381836
]
]
], // <--- missing this
[ // <--- missing this
[
[
43.79203909839882,
-79.4476318359375
],
[
43.80380985089954,
-79.39647674560547
],
[
43.76334592336985,
-79.38712120056152
],
[
43.76179622406369,
-79.39501762390137
],
[
43.73600333614323,
-79.43381309509277
],
[
43.79203909839882,
-79.4476318359375
]
]
]
]
}
but you were missing a layer of closing and opening of arrays that I have highlighted. The structures are big and nested so it's hard to eyeball what is going on; I'd suggest using tools like jsonlint and geojsonlint to contrast my corrected structure with your previous structure. It's the only way I was able to spot the problem.

Resources