How can i filter an array of json in swift having only certain key value pairs?
my array looks like:
[{
"status" : "true",
"score" : "3",
"correct" : "3",
"chapter" : "34",
"answer" : "342432",
"solutionText" : "abcd",
}, {
"status" : "true",
"score" : "0",
"correct" : "2",
"chapter" : "35",
"answer" : "35854",
"solutionText" : "abc",
}]
i want to get an array of json output having only status, chapter & correct key value pairs.
Like:
[{
"status" : "true",
"correct" : "3",
"chapter" : "34",
}, {
"status" : "true",
"correct" : "2",
"chapter" : "35",
}]
Considering this is your JSON
var myJSON = """
[{
"status" : "true",
"score" : "3",
"correct" : "3",
"chapter" : "34",
"answer" : "342432",
"solutionText" : "abcd"
}, {
"status" : "true",
"score" : "0",
"correct" : "2",
"chapter" : "35",
"answer" : "35854",
"solutionText" : "abc"
}]
"""
Simply create a Decodable struct like this
typealias MyArray = [MyObject] // Use this to decode
struct MyObject: Codable {
let status, correct, chapter: String
}
And use it like this
//Usage
var myJSONData = myJSON.data(using: .utf8)! // converting the JSON to data
let objArray = try! JSONDecoder().decode(MyArray.self, from: myJSONData) // decoding the json data into an object
//how to access
print(objArray.count)// number of elements in my array
print(objArray.first!) // getting the first object
let myObject = obj[0] // also getting the first object by index
myObject.chapter
myObject.correct
myObject.status
Read about Codable here .
If it's already in an object, you can try
filteredArray = myArray.map { ["status": $0.status, "correct": $0.correct, "chapter": $0.chapter] }
You can try
do {
let res = try JSONDecoder().decode([Root].self, from:data)
}
catch {
print(error)
}
struct Root: Codable {
let status, correct, chapter: String
}
Correct json
[{
"status" : "true",
"score" : "3",
"correct" : "3",
"chapter" : "34",
"answer" : "342432",
"solutionText" : "abcd"
}, {
"status" : "true",
"score" : "0",
"correct" : "2",
"chapter" : "35",
"answer" : "35854",
"solutionText" : "abc"
}]
It's more suitable to make
status a bool not string
correct & chapter to be integers
so json look like
[{
"status" : true,
"score" : "3",
"correct" : 3,
"chapter" : 34,
"answer" : "342432",
"solutionText" : "abcd"
}, {
"status" : true,
"score" : "0",
"correct" : 2,
"chapter" : 35,
"answer" : "35854",
"solutionText" : "abc"
}]
you can also make other values like that if you need , then your model will look like
struct Root: Codable {
let status: Bool
let correct, chapter: Int
}
Edit:
let data = try content.rawData() // this inside do block
where content is of type JSON
try Decodable of swift
class MyObjectsClass : Decodable {
var objects : [Objects]?
}
class Objects : Decodable {
var status : String?
var correct : String?
var chapter : String?
}
While Decoding
let decodedValue = try? JSONDecoder.decode(MyObjectsClass.self, from : data)
// where data from API calls
Related
So the JSON variable let json = JSON(nearbyChargingSites.jsonString!) contains the current data.:
{
"timestamp" : 1626902257093,
"superchargers" : [
{
"location" : {
"lat" : 63.325319,
"long" : 10.305137
},
"total_stalls" : 19,
"distance_miles" : 10.064082000000001,
"type" : "supercharger",
"site_closed" : false,
"available_stalls" : 15,
"name" : "Leinstrand, Norway - Klett"
},
{
"location" : {
"lat" : 63.466445999999998,
"long" : 10.91766
},
"total_stalls" : 16,
"distance_miles" : 11.838984999999999,
"type" : "supercharger",
"site_closed" : false,
"available_stalls" : 16,
"name" : "Stjørdal, Norway"
},
{
"location" : {
"lat" : 63.734355000000001,
"long" : 11.281487
},
"total_stalls" : 12,
"distance_miles" : 31.206503999999999,
"type" : "supercharger",
"site_closed" : false,
"available_stalls" : 11,
"name" : "Levanger, Norway"
},
{
"location" : {
"lat" : 62.832030000000003,
"long" : 10.009639999999999
},
"total_stalls" : 20,
"distance_miles" : 44.117753,
"type" : "supercharger",
"site_closed" : false,
"available_stalls" : 17,
"name" : "Berkåk, Norway"
}
],
"congestion_sync_time_utc_secs" : 1626902199,
"destination_charging" : [
{
"distance_miles" : 23.366278999999999,
"name" : "Bårdshaug Herregård",
"location" : {
"lat" : 63.299208,
"long" : 9.8448650000000004
},
"type" : "destination"
},
{
"distance_miles" : 38.390034,
"name" : "Fosen Fjordhotel",
"location" : {
"lat" : 63.959356999999997,
"long" : 10.223908
},
"type" : "destination"
},
{
"distance_miles" : 46.220022999999998,
"name" : "Falksenteret",
"location" : {
"lat" : 63.293301999999997,
"long" : 9.0834460000000004
},
"type" : "destination"
},
{
"distance_miles" : 54.981445000000001,
"name" : "Væktarstua",
"location" : {
"lat" : 62.908683000000003,
"long" : 11.893306000000001
},
"type" : "destination"
}
]
}
I use SwiftyJSON and tries to get the superchargers latitude, longitude and name, like this:
let jsonName = json["superchargers"]["name"]
let jsonLat = json["superchargers"]["location"]["lat"]
let jsonLong = json["superchargers"]["location"]["long"]
When trying to print any of those, all of them return nil.
Any ideas what I am doing wrong, and how to do this?
The reason I want to do this is because I want to add them as annotation to a MKMapView.
The first error is that the JSON initializer you are using will create a single JSON string object, it will not parse the string as JSON data:
Instead of:
let json = JSON(nearbyChargingSites.jsonString!)
you need to use:
let json = JSON(data: dataFromJSONString)
Second you need to iterate over the superchargers array to collect all the values
Try something like:
if let dataFromString = nearbyChargingSites.jsonString!.data(using: .utf8, allowLossyConversion: false) {
let json = try! JSON(data: dataFromString,options: .allowFragments)
for supercharger in json["superchargers"].arrayValue {
let jsonName = supercharger["name"].stringValue
let jsonLat = supercharger["location"]["lat"].doubleValue
let jsonLong = supercharger["location"]["long"].doubleValue
}
}
Please note that the above code does not perform error handling and will crash if values are missing from JSON.
I have a simple test database that I cant get to filter. I indexed the category in the rules:
"questions":{
".indexOn": ["category"]
},
My filter for the quiz app:
/questions.json?orderBy="category"&equalTo="Basics"&print=pretty
and my database:
"-MKoucSP33zm4jC43AnY" : {
"title" : {
"answers" : [ {
"score" : 30,
"text" : "Pineapple"
}, {
"score" : 5,
"text" : "Ham"
}, {
"score" : 20,
"text" : "Yogurt"
}, {
"score" : 10,
"text" : "Crab"
} ],
"category" : "Basics",
"questionId" : "101",
"questionImage" : "",
"questionLink" : "",
"questionText" : "What topping do you like the best on pizza?"
}
}
The category property is nested under the title node, so the property you need to order/filter on is title/category:
/questions.json?orderBy="title/category"&equalTo="Basics"&print=pretty
You'll also need to update your index definition for that path, so:
"questions": { ".indexOn": "title/category" }
Working example: https://stackoverflow.firebaseio.com/64596200/questions.json?orderBy="title/category"&equalTo="Basics"
Is it possible to create Salesreceipt without product/service value through QBO API? I have tried through API but it's not reflecting rate value and storing description value only.
If I remove ItemRef attribute(in request body) then it's reflecting rate and amount values and it's assigning some default and random product/service.
It is possible directly in QBO UI.
Request body where only description value storing:
{
"TxnDate" : "2016-05-27",
"Line" : [ {
"Amount" : 2222.00,
"Description" : "hi chk",
"DetailType" : "ItemReceiptLineDetail",
"ItemReceiptLineDetail" : {
"ItemRef" : { },
"Qty" : 1,
"UnitPrice" : 2222
} }
],
"CustomerRef" : {
"value" : "67"
},
"CustomerMemo" : {
"value" : "Thanks for your business! We appreciate referrals!"
},
"TotalAmt": 2222.00,
"PrivateNote" : "",
"CustomField" : [ {
"DefinitionId" : "1",
"Type" : "StringType",
"StringValue" : ""
} ]
}
Request body where default product/service assigning:
{
"TxnDate" : "2016-05-27",
"Line" : [ {
"Amount" : 2222.00,
"Description" : "hi chk",
"DetailType" : "ItemReceiptLineDetail",
"ItemReceiptLineDetail" : {
"Qty" : 1,
"UnitPrice" : 2222
} }
],
"CustomerRef" : {
"value" : "67"
},
"CustomerMemo" : {
"value" : "Thanks for your business! We appreciate referrals!"
},
"TotalAmt": 2222.00,
"PrivateNote" : "",
"CustomField" : [ {
"DefinitionId" : "1",
"Type" : "StringType",
"StringValue" : ""
} ]
}
No.
QuickBooks Online does not support this.
I've read many articles on SO and I'm still stumped! I have an array of dictionaries, see below:
var myArray = [[String:AnyObject]]()
myArray.append([
"caseNumber" : "12349",
"formType" : "Advanced",
"caseStatus" : "Approved",
"caseDetails" : "blah blah",
"caseLUD" : NSDate(),
"friendlyName" : "name1"
])
myArray.append([
"caseNumber" : "12345",
"formType" : "Standard",
"caseStatus" : "On-Hold",
"caseDetails" : "blah blah",
"caseLUD" : NSDate(),
"friendlyName" : "name2"
])
myArray.append([
"caseNumber" : "12342",
"formType" : "Normal",
"caseStatus" : "Rejected",
"caseDetails" : "blah blah",
"caseLUD" : NSDate(),
"friendlyName" : "name3"
])
This data will go into a tableView and I want the user to be able to decide the sort order, either by caseNumber or my caseLUD (date). But I can't figure out how to re-order the array by values. Any ideas, please? All help appreciated.
To sort by caseNumber:
myArray.sortInPlace { ($0["caseNumber"] as! String) < ($1["caseNumber"] as! String) }
Note, this uses caseNumber as a String which could lead to surprising results (for example "12345" < "2"). You might either want to store caseNumber as an Int, or convert it to an Int when sorting:
myArray.sortInPlace { Int($0["caseNumber"] as! String) < Int($1["caseNumber"] as! String) }
To sort by caseLUD:
myArray.sortInPlace { ($0["caseLUD"] as! NSDate).compare($1["caseLUD"] as! NSDate) == .OrderedAscending}
.OrderedAscending will give you earliest date first, and .OrderedDescending will give you latest date first.
If you stored your data in a Struct instead of a Dictionary, you could avoid the messy typecasting.
struct CaseRecord {
var caseNumber: Int
var formType: String
var caseStatus: String
var caseDetails: String
var caseLUD: NSDate
var friendlyName: String
}
var myArray = [CaseRecord]()
myArray.append(CaseRecord(
caseNumber : 12345,
formType : "Advanced",
caseStatus : "Approved",
caseDetails : "blah blah",
caseLUD : NSDate(),
friendlyName : "name1"
)
)
myArray.append(CaseRecord(
caseNumber : 124,
formType : "Standard",
caseStatus : "On-Hold",
caseDetails : "blah blah",
caseLUD : NSDate(),
friendlyName : "name2"
)
)
myArray.append(CaseRecord(
caseNumber : 13,
formType : "Normal",
caseStatus : "Rejected",
caseDetails : "blah blah",
caseLUD : NSDate(),
friendlyName : "name3"
)
)
myArray.sortInPlace { $0.caseNumber < $1.caseNumber }
myArray.sortInPlace { $0.caseLUD.compare($1.caseLUD) == .OrderedDescending }
I want to retrieve data from a JSON file with SwiftyJSON and Alamofire. This is the JSON data:
json: {
"data" : {
"monitors" : [
{
"lines" : [
{
"towards" : "LEOPOLDAU",
"realtimeSupported" : true,
"departures" : {
"departure" : [
{
"departureTime" : {
"countdown" : 2,
"timePlanned" : "2015-09-09T18:47:03.000+0200",
"timeReal" : "2015-09-09T18:47:03.000+0200"
}
},
{
"departureTime" : {
"countdown" : 6,
"timePlanned" : "2015-09-09T18:51:03.000+0200",
"timeReal" : "2015-09-09T18:51:03.000+0200"
}
}
]
},
"lineId" : 301,
"platform" : "1",
"trafficjam" : false,
"richtungsId" : "1",
"direction" : "H",
"type" : "ptMetro",
"name" : "U1",
"barrierFree" : true
}
],
"attributes" : {
},
"locationStop" : {
"type" : "Feature",
"geometry" : {
"type" : "Point",
"coordinates" : [
16.4330680046932,
48.2430825589716
]
},
"properties" : {
"municipality" : "Wien",
"title" : "Kagran",
"coordName" : "WGS84",
"municipalityId" : 90000,
"type" : "stop",
"gate" : "1",
"name" : "60200627",
"attributes" : {
"rbl" : 4127
}
}
}
}
]
}
,
"message" : {
"value" : "OK",
"serverTime" : "2015-09-09T18:45:08.998+0200",
"messageCode" : 1
}
}
What I want to do now is get the data of lines into an array. I tried several combinations for that, e.g.
let departureArray = jsonData["data"]["monitors"][0]["lines"][0].array
or
let departureArray = jsonData["data"]["monitors"]["lines"].array
but my array is always empty. Does anyone has an idea how to do this properly?
Your expected array departureArray is in fact a dictionary
let departureDictionary = jsonData["data"]["monitors"][0]["lines"][0].dictionary