How do take the values from a large JSON response with multiple arrays and brackets? - ios

I have a JSON response (over 500lines) , that has several arrays and brackets inside. Basically an overly nested response.
I am using Alamofire for fetching and i get a JSONData as response. But I don't know how to take values from the response and print it on a custom cell .
I am using Alamofire 5 to get the JSON response.
Please excuse if this sounds too newbieish , I am new to iOS developing.
{
"availabilityResultList": [
{
"availabilityRouteList": [
{
"availabilityByDateList": [
{
"originDestinationOptionList": [
{
"fareComponentGroupList": [
{
"boundList": [
{
"availFlightSegmentList": [
{
"addOnSegment": false,
"flightSegment": {
"journeyDuration": "PT1H15M",
"distance": 1,
"departureAirport": {
"locationName": "LOC",
"cityInfo": {
"country": {
"locationName": "LOC",
"currency": {
"code": "USD"
},
"locationCode": "LOC",
"locationNameLanguage": "EN"
},
"city": {
"locationName": "LOC",
"locationCode": "LOC",
"locationNameLanguage": "EN"
}
},
"codeContext": "ASD",
"language": "EN",
"locationCode": "LOC"
},
"equipment": {
"airEquipType": "ABC",
"changeofGauge": false
},
"remark": "NO",
"ticketType": "PP",
"onTimeRate": 0,
"flightNotes": [
{
"deiCode": 0
}
],
"flightSegmentID": "",
"flownMileageQty": 0,
"flightNumber": "106",
"groundDuration": "",
"trafficRestriction": {
"code": "",
"explanation": {}
},
"stopQuantity": 0,
"codeshare": false,
"secureFlightDataRequired": false,
"departureDateTime": {
"month": 5,
"hour": 6,
"year": 2019,
"timezone": 270,
"day": 6,
"minute": 45,
"second": 0
},
"ondControlled": false,
"arrivalDateTime": {
"month": 5,
"hour": 8,
"year": 2019,
"timezone": 270,
"day": 6,
"minute": 0,
"second": 0
},
"airline": {
"code": "RQ"
},
"arrivalAirport": {
"locationName": "LOC",
"cityInfo": {
"country": {
"locationName": "AA",
"currency": {
"code": "USD"
},
"locationCode": "AA",
"locationNameLanguage": "EN"
},
"city": {
"locationName": "loc",
"locationCode": "LOC",
"locationNameLanguage": "EN"
}
},
"codeContext": "ASD",
"language": "EN",
"locationCode": "LOC"
},
"sector": "ASD",
"accumulatedDuration": ""
},
"marriageGroup": "-1"
}
],
"boundCode": "Outbound"
}
]
}
]
},

JSON is a very simple format and easy to read. There are only two different collection types and four value types.
{} is a dictionary and is subscripted by key
[] is an array and is subscripted by index (zero-based)
Everything in double quotes is String, even "123" and "false"
false and true is Bool
Numeric values are Double, without fractional digits Int
<null> is NSNull
With SwiftyJSON you get distance in flightSegment with (root represents the top level object`)
root["availabilityResultList"][0]["availabilityRouteList"][0]["availabilityByDateList"][0]["originDestinationOptionList"][0]["fareComponentGroupList"][0]["boundList"][0]["availFlightSegmentList"][0]["flightSegment"]["distance"]

Related

Elastic search running in docker container does not return nearest neighbour results as expected following an ANN query

I am following the approximate nearest neighbour (ANN) task found at:
https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#approximate-knn
and I am running Elastic Search using the insecure mode via docker compose, but the results of the ANN query are never close to the provided vector and seem to return the 'bulk' inserted vectors in the same order. The exact match never appears first, as I would reasonably expect it to.
I spin up ES using the following docker compose file:
version: "3.7"
services:
es:
container_name: ann_es
image: docker.elastic.co/elasticsearch/elasticsearch:8.6.0
environment:
- discovery.type=single-node
- xpack.security.enabled=false
ports:
- "9200:9200"
Then I insert an index operating over a dense_vector
PUT https://localhost:9200/image-index
{
"mappings": {
"properties": {
"image-vector": {
"type": "dense_vector",
"dims": 3,
"index": true,
"similarity": "l2_norm"
},
"title": {
"type": "text"
},
"file-type": {
"type": "keyword"
}
}
}
}
I've noticed that when I GET the index, the mappings are not present. Not sure if this is the root of the problem, but after I insert data (see later), a basic mapping appears that's not the one I created using a dense_vector.
GET http://localhost:9200/image-index
{
"image-index": {
"aliases": {},
"mappings": **{}**,
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "image-index",
"creation_date": "1675178096730",
"number_of_replicas": "1",
"uuid": "HR521T_jQfeduY7cdMr-Jw",
"version": {
"created": "8060099"
}
}
}
}
}
I have a programme that will insert 10000 random vectors similar to this:
POST http://localhost:9200/image-index/_bulk?refresh=true
{ "index": { "_id": "1" } }
{ "image-vector": [1, 5, -20], "title": "moose family", "file-type": "jpg" }
{ "index": { "_id": "2" } }
{ "image-vector": [42, 8, -15], "title": "alpine lake", "file-type": "png" }
{ "index": { "_id": "3" } }
{ "image-vector": [15, 11, 23], "title": "full moon", "file-type": "jpg" }
When I retrieve the index I can see that the index has a mapping (but it's not a dense_vector)
GET http://localhost:9200/image-index
{
"image-index": {
"aliases": {},
"mappings": {
"properties": {
"file-type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"image-vector": {
"type": "long"
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "image-index",
"creation_date": "1675178096730",
"number_of_replicas": "1",
"uuid": "HR521T_jQfeduY7cdMr-Jw",
"version": {
"created": "8060099"
}
}
}
}
}
When I invoke an ANN search query:
GET http://localhost:9200/image-index/_search
{
"knn": {
"field": "image-vector",
"query_vector": [42, 8, -15],
"k": 10,
"num_candidates": 1
},
"fields": [ "title", "file-type" ]
}
I always get totally inaccurate results, no matter how many vectors I insert or if I fix the query to be obviously near one of the many vectors I insert:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "image-index",
"_id": "1",
"_score": 1.0,
"_source": {
"image-vector": [
1,
5,
-20
],
"title": "moose family",
"file-type": "jpg"
}
},
{
"_index": "image-index",
"_id": "2",
"_score": 1.0,
"_source": {
"image-vector": [
42,
8,
-15
],
"title": "alpine lake",
"file-type": "png"
}
},
{
"_index": "image-index",
"_id": "3",
"_score": 1.0,
"_source": {
"image-vector": [
15,
11,
23
],
"title": "full moon",
"file-type": "jpg"
}
}
]
}
}
Does anybody know what I am doing wrong?

Schema definition in Swagger

I'm very new to OpenAPI and I'm using http://editor.swagger.io to design an API.
I'm stuck in Schema with a JSON looking like following
{
"CORRELATION_ID": "10",
"CONTROL":
{
"DAS_IS_RECIPIENT": "123",
"DOCTPYE": "ert",
"PROCESS_INDICATOR": "nord"
},
"HEADER":
{
"ID": "456",
"INVOICE_NUMBER": "678",
"DMS_DOC_ID": "876",
"INVOICE_DATE": "10082020"
},
"ITEMS": [
{
"SHORT_TEXT": "123",
"LSTAR": 0,
"QUANTITY": "23"
},
{
"SHORT_TEXT": "456",
"LSTAR": 234,
"QUANTITY": "21"
}
],
"DEBITOR":
{
"ID": "444",
"FIRSTNAME": "nick",
"LASTNAME": "cantre"
},
"CREDITOR":
{
"ID": "454",
"FIRSTNAME": "ava",
"LASTNAME": "pierre"
}
}
How to create a schema according to this JSON structure?

How to sum items in a List in Dart

What's the best way to sum all the "amounts" of all "expenses", for each user?
I've tried a few different things but I can't quite get it right. It should return 2 values: 20.0 and 24.90
[
{
"id": 3,
"company": {
"id": 2
},
"user": {
"id": 3,
"first_name": "Fred",
"last_name": "Smith",
"email": "asdfasf",
"is_suspended": false,
"vendor_id": "FS-100",
"username": "etytyurtyu",
"expense": [
{
"id": 7,
"date": "2019-12-14T00:00:00.000Z",
"amount": 20.0,
"payment_type": "companyAccount",
"last_modified": "2019-12-16T23:50:00.459064Z",
"receipt_uri": [
"URL8",
"URL9"
],
"user": {
"id": 3
},
"expense_type": {
"id": 4
},
"booking": {
"id": "HI-3565346"
}
}
]
}
},
{
"id": 2,
"company": {
"id": 2
},
"user": {
"id": 2,
"first_name": "Pierre",
"last_name": "XXXMar",
"email": "asdfasdfads",
"is_suspended": false,
"vendor_id": "PM-100",
"username": "asdfas",
"expense": [
{
"id": 2,
"date": "2019-12-16T00:00:00.000Z",
"amount": 12.45,
"payment_type": "provided",
"last_modified": "2019-12-16T19:01:37.092932Z",
"receipt_uri": [
"URL1"
],
"user": {
"id": 2
},
"expense_type": {
"id": 6
},
"booking": {
"id": "MU-123414"
}
},
{
"id": 5,
"date": "2019-12-08T00:00:00.000Z",
"amount": 12.45,
"payment_type": "provided",
"last_modified": "2019-12-16T23:50:00.459064Z",
"receipt_uri": [
"URL1"
],
"user": {
"id": 2
},
"expense_type": {
"id": 6
},
"booking": {
"id": "MU-123414"
}
},
{
"id": 3,
"date": "2019-12-17T00:00:00.000Z",
"amount": 20.0,
"payment_type": "companyCard",
"last_modified": "2019-12-16T19:01:37.092932Z",
"receipt_uri": [
"URL5",
"URL6"
],
"user": {
"id": 2
},
"expense_type": {
"id": 12
},
"booking": {
"id": "HI-3565346"
}
}
]
}
}
]
Thanks
You would first map the expenses amounts, then fold it into a single value, like this :
double sum = expenses.map((expense) => expense.amount).fold(0, (prev, amount) => prev + amount);
List item
For the sake of completeness: In 2021 you can replace the .fold(0, (prev, amount) => prev + amount) part by just calling .sum on the list. Just import import 'package:collection/collection.dart'; first –
tmaihoff

How do I remove optional text from UILabel

I'm trying to remove the Optional text on my UILabel when I run the app. However, I already tried many ways and the Optional persists.
It's important to say that I got these values from JSON, so I created a Struct to decode all this Data. The property ibu is the only one that has Optional written, but if I remove the ? I got an error on JSONDecoder that say's:
Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index
23", intValue: 23), CodingKeys(stringValue: "ibu", intValue: nil)],
debugDescription: "Expected Double value but found null instead.",
underlyingError: nil))
How do I fix that?
App Picture:
Model Folder:
struct Cerveja:Decodable{
let name:String
let image_url:String
let description:String
let tagline:String
let abv:Double
let ibu:Double? //This one that I got the "Optional" written in the UILabel
//If I remove the "?" I got the error below
}
View Folder - Details Screen:
var item:Cerveja?
override func viewDidLoad() {
super.viewDidLoad()
labelName.text = item?.name
labelDescricao.text = item?.description
labelAmargor.text = "\(String(describing: item!.ibu))"
labelTeorAlc.text = "\(item!.abv)"
let resource = ImageResource(downloadURL: URL(string: "\(item?.image_url ?? "")")!, cacheKey: item?.image_url)
imageDetail.kf.setImage(with: resource)
}
Networking Folder:
func getApiData(completion: #escaping ([Cerveja]) -> ()){
guard let urlString = URL(string: "https://api.punkapi.com/v2/beers") else {
print("URL Error")
return
}
Alamofire.request(urlString).responseJSON { response in
if response.data == response.data{
do{
let decoder = try JSONDecoder().decode([Cerveja].self, from: response.data!)
completion(decoder)
}catch{
print(error)
}
}else{print("API Response is Empty")}
}
}
JSON:
[
{
"id": 1,
"name": "Buzz",
"tagline": "A Real Bitter Experience.",
"first_brewed": "09\/2007",
"description": "A light, crisp and bitter IPA brewed with English and American hops. A small batch brewed only once.",
"image_url": "https:\/\/images.punkapi.com\/v2\/keg.png",
"abv": 4.5,
"ibu": 60,
"target_fg": 1010,
"target_og": 1044,
"ebc": 20,
"srm": 10,
"ph": 4.4,
"attenuation_level": 75,
"volume": {
"value": 20,
"unit": "liters"
},
"boil_volume": {
"value": 25,
"unit": "liters"
},
"method": {
"mash_temp": [
{
"temp": {
"value": 64,
"unit": "celsius"
},
"duration": 75
}
],
"fermentation": {
"temp": {
"value": 19,
"unit": "celsius"
}
},
"twist": null
},
"ingredients": {
"malt": [
{
"name": "Maris Otter Extra Pale",
"amount": {
"value": 3.3,
"unit": "kilograms"
}
},
{
"name": "Caramalt",
"amount": {
"value": 0.2,
"unit": "kilograms"
}
},
{
"name": "Munich",
"amount": {
"value": 0.4,
"unit": "kilograms"
}
}
],
"hops": [
{
"name": "Fuggles",
"amount": {
"value": 25,
"unit": "grams"
},
"add": "start",
"attribute": "bitter"
},
{
"name": "First Gold",
"amount": {
"value": 25,
"unit": "grams"
},
"add": "start",
"attribute": "bitter"
},
{
"name": "Fuggles",
"amount": {
"value": 37.5,
"unit": "grams"
},
"add": "middle",
"attribute": "flavour"
},
{
"name": "First Gold",
"amount": {
"value": 37.5,
"unit": "grams"
},
"add": "middle",
"attribute": "flavour"
},
{
"name": "Cascade",
"amount": {
"value": 37.5,
"unit": "grams"
},
"add": "end",
"attribute": "flavour"
}
],
"yeast": "Wyeast 1056 - American Ale\u2122"
},
"food_pairing": [
"Spicy chicken tikka masala",
"Grilled chicken quesadilla",
"Caramel toffee cake"
],
"brewers_tips": "The earthy and floral aromas from the hops can be overpowering. Drop a little Cascade in at the end of the boil to lift the profile with a bit of citrus.",
"contributed_by": "Sam Mason <samjbmason>"
},
{
"id": 2,
"name": "Trashy Blonde",
"tagline": "You Know You Shouldn't",
"first_brewed": "04\/2008",
"description": "A titillating, neurotic, peroxide punk of a Pale Ale. Combining attitude, style, substance, and a little bit of low self esteem for good measure; what would your mother say? The seductive lure of the sassy passion fruit hop proves too much to resist. All that is even before we get onto the fact that there are no additives, preservatives, pasteurization or strings attached. All wrapped up with the customary BrewDog bite and imaginative twist.",
"image_url": "https:\/\/images.punkapi.com\/v2\/2.png",
"abv": 4.1,
"ibu": 41.5,
"target_fg": 1010,
"target_og": 1041.7,
"ebc": 15,
"srm": 15,
"ph": 4.4,
"attenuation_level": 76,
"volume": {
"value": 20,
"unit": "liters"
},
"boil_volume": {
"value": 25,
"unit": "liters"
},
"method": {
"mash_temp": [
{
"temp": {
"value": 69,
"unit": "celsius"
},
"duration": null
}
],
"fermentation": {
"temp": {
"value": 18,
"unit": "celsius"
}
},
"twist": null
},
"ingredients": {
"malt": [
{
"name": "Maris Otter Extra Pale",
"amount": {
"value": 3.25,
"unit": "kilograms"
}
},
{
"name": "Caramalt",
"amount": {
"value": 0.2,
"unit": "kilograms"
}
},
{
"name": "Munich",
"amount": {
"value": 0.4,
"unit": "kilograms"
}
}
],
"hops": [
{
"name": "Amarillo",
"amount": {
"value": 13.8,
"unit": "grams"
},
"add": "start",
"attribute": "bitter"
},
{
"name": "Simcoe",
"amount": {
"value": 13.8,
"unit": "grams"
},
"add": "start",
"attribute": "bitter"
},
{
"name": "Amarillo",
"amount": {
"value": 26.3,
"unit": "grams"
},
"add": "end",
"attribute": "flavour"
},
{
"name": "Motueka",
"amount": {
"value": 18.8,
"unit": "grams"
},
"add": "end",
"attribute": "flavour"
}
],
"yeast": "Wyeast 1056 - American Ale\u2122"
},
"food_pairing": [
"Fresh crab with lemon",
"Garlic butter dipping sauce",
"Goats cheese salad",
"Creamy lemon bar doused in powdered sugar"
],
"brewers_tips": "Be careful not to collect too much wort from the mash. Once the sugars are all washed out there are some very unpleasant grainy tasting compounds that can be extracted into the wort.",
"contributed_by": "Sam Mason <samjbmason>"
},
{
"id": 3,
"name": "Berliner Weisse With Yuzu - B-Sides",
"tagline": "Japanese Citrus Berliner Weisse.",
"first_brewed": "11\/2015",
"description": "Japanese citrus fruit intensifies the sour nature of this German classic.",
"image_url": "https:\/\/images.punkapi.com\/v2\/keg.png",
"abv": 4.2,
"ibu": 8,
"target_fg": 1007,
"target_og": 1040,
"ebc": 8,
"srm": 4,
"ph": 3.2,
"attenuation_level": 83,
"volume": {
"value": 20,
"unit": "liters"
},
"boil_volume": {
"value": 25,
"unit": "liters"
},
"method": {
"mash_temp": [
{
"temp": {
"value": 60,
"unit": "celsius"
},
"duration": 10
},
{
"temp": {
"value": 65,
"unit": "celsius"
},
"duration": 30
},
{
"temp": {
"value": 72,
"unit": "celsius"
},
"duration": 10
},
{
"temp": {
"value": 78,
"unit": "celsius"
},
"duration": 5
}
],
"fermentation": {
"temp": {
"value": 21,
"unit": "celsius"
}
},
"twist": "Soured naturally using the kettle souring technique, Yuzu fruit: 50g at middle, Yuzu juice: 200ml at FV"
},
"ingredients": {
"malt": [
{
"name": "Propino Pale Malt",
"amount": {
"value": 1.63,
"unit": "kilograms"
}
},
{
"name": "Wheat Malt",
"amount": {
"value": 1.63,
"unit": "kilograms"
}
},
{
"name": "Propino Pale Malt for kettle 0.03kg souring",
"amount": {
"value": 0.03,
"unit": "kilograms"
}
},
{
"name": "Acidulated Malt for kettle souring",
"amount": {
"value": 0.03,
"unit": "kilograms"
}
}
],
"hops": [
{
"name": "Bramling Cross",
"amount": {
"value": 10,
"unit": "grams"
},
"add": "middle",
"attribute": "bitter"
}
],
"yeast": "Wyeast 1056 - American Ale\u2122"
}
Use optional binding to avoid this problem
Use
if let ibu = item?.ibu {
labelAmargor.text = "\(String(describing: ibu))"
}
Instead of
labelAmargor.text = "\(String(describing: item!.ibu))"
I would recommend Nil-Coalescing Operator ( ?? ). You can find detailed info here
class Test {
var ibu: Double?
}
var tt: Test? = Test()
tt?.ibu = 3.14
print(String(describing: tt?.ibu)) // Optional("3.14")
print(tt?.ibu ?? 0) // 3.14
tt?.ibu = nil
print(String(describing: tt?.ibu)) // nil
print(tt?.ibu ?? 0) // 0

How do I create (or update) a QBO BillPayment so that it has an unapplied payment amount?

I created a bill through the QuickBooks Online (QBO) web UI. Then I queried using the API (v3, documented here). The response:
{
"SyncToken": "16",
"domain": "QBO",
"VendorRef": {
"name": "MyVendor",
"value": "237"
},
"TxnDate": "2014-12-07",
"TotalAmt": 1.83,
"CurrencyRef": {
"name": "United States Dollar",
"value": "USD"
},
"PayType": "Check",
"PrivateNote": "test billpayment description (mod)",
"sparse": false,
"Line": [
{
"Amount": 1.22,
"LinkedTxn": [
{
"TxnId": "1412",
"TxnType": "Bill"
}
]
}
],
"Id": "1413",
"CheckPayment": {
"PrintStatus": "NeedToPrint",
"BankAccountRef": {
"name": "MyBankAcct#1234",
"value": "137"
}
},
"MetaData": {
"CreateTime": "2014-12-07T18:44:51-08:00",
"LastUpdatedTime": "2014-12-10T20:20:28-08:00"
}
}
As you can see, it has $0.61 of the TotalAmt unapplied to any Bills. (The other $1.22 is applied to Bill 1412.) Now, when I try to update this bill to change JUST the amount applied to the linked transaction, I get unexpected results.
Here's the update request body:
{
"SyncToken": "16",
"domain": "QBO",
"VendorRef": {
"name": "MyVendor",
"value": "237"
},
"TxnDate": "2014-12-07",
"TotalAmt": 1.83,
"CurrencyRef": {
"name": "United States Dollar",
"value": "USD"
},
"PayType": "Check",
"PrivateNote": "test billpayment description (mod)",
"sparse": false,
"Line": [
{
"Amount": 1.21,
"LinkedTxn": [
{
"TxnId": "1412",
"TxnType": "Bill"
}
]
}
],
"Id": "1413",
"CheckPayment": {
"PrintStatus": "NeedToPrint",
"BankAccountRef": {
"name": "MyBankAcct#1234",
"value": "137"
}
},
"MetaData": {
"CreateTime": "2014-12-07T18:44:51-08:00",
"LastUpdatedTime": "2014-12-10T20:20:28-08:00"
}
}
In the response, it appears that the API just basically can't handle the situation (even though it clearly existed when I created it in the web UI). Look what happens to the TotalAmt attribute! It's reduced to whatever the payment was in the LinkedTxn:
{
"BillPayment": {
"VendorRef": {
"value": "237",
"name": "MyVendor"
},
"PayType": "Check",
"CheckPayment": {
"BankAccountRef": {
"value": "137",
"name": "MyBankAcct#1234"
},
"PrintStatus": "NeedToPrint"
},
"TotalAmt": 1.21,
"domain": "QBO",
"sparse": false,
"Id": "1413",
"SyncToken": "17",
"MetaData": {
"CreateTime": "2014-12-07T18:44:51-08:00",
"LastUpdatedTime": "2014-12-10T20:25:02-08:00"
},
"TxnDate": "2014-12-07",
"CurrencyRef": {
"value": "USD",
"name": "United States Dollar"
},
"PrivateNote": "test billpayment description (mod)",
"Line": [
{
"Amount": 1.21,
"LinkedTxn": [
{
"TxnId": "1412",
"TxnType": "Bill"
}
]
}
]
},
"time": "2014-12-10T20:25:01.91-08:00"
}
I don't see discussion of this on the known issues page, so I'm pretty confused.
If you have any suggestions as to how to get this to do what I want it to, I thank you for your thoughts!

Resources