filter array of json in swift
swift filter array of strings
filter array of array swift
swift filter array of objects by property
filter array swift
remove duplicate objects from array swift 4
swift array contains object with property
swift array of objects
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 .
Swift techniques to filter duplicates out of large JSON arrays � aplus.rs, struct Entity { let id: Int64 var name: String var flags: [Int] } In the array, some of the JSON objects may be duplicated where id is the same but� The filter(isIncluded:)method takes a closure that is executed for every element in the source Array. If you return true from this closure, the element will be included in a new, filtered Array. If you return false, the element is ignored and won't be included in the new Array.
If it's already in an object, you can try
filteredArray = myArray.map { ["status": $0.status, "correct": $0.correct, "chapter": $0.chapter] }
Swift example. Where we have an array of JSON object, each object , Swift example. Where we have an array of JSON object, each object can have " type" and we are about to use this function to decode objects of� JSON Parsing in Swift explained with code examples. JSON parsing in Swift is a common thing to do. Almost every app decodes JSON to show data in a visualized way. Parsing JSON is definitely one of the basics you should learn as an iOS developer. Decoding JSON in Swift is quite easy and does not require any external dependencies.
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
Filtering an array of JSON objects by multiple properties : javascript, Array filter? Array map? Array reduce? None of these? I was looping the objects and setting the visible property of the object to either true or false. A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element should be included in the returned array. Return Value An array of the elements that is Included allowed.
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
Filtering array of objects by strings in another array. : swift, I also have an array of strings. I would like to remove the Match object where matchID equals any string in the array. I currently have: var matches = [Match]� Your array, objects, is an array of PFObject objects. Thus, to filter the array, you might do something like: let filteredArray = objects.filter () { if let type = ($0 as PFObject) ["Type"] as String { return type.rangeOfString ("Sushi") != nil } else { return false } }
SOLVED: Filter whole custom JSON object for , I am using the below code to filter the array contents. It works for a single property , in this case propertyToFilter . How to extend this functionality� A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element should be included in the returned collection.
How to filter nil values out of the collection types— Swift, Array. For example, let's take a look at the following arrays: First one is nullable, and the second one is non-nullable let� Manually Finding An Item In An Array. Before we dive into Swift’s ways you can find items in an array, let’s find out how such an algorithm actually works. This will definitely help your understanding of algorithms and iOS development in general.
how to filter json object in swift?, How can I filter these json object? I mean I want print only patients whose id is equal to 3. var patients: Array<AnyObject>? if let obj: AnyObject = manager? Specifically, you use the Array type to hold elements of a single type, the array’s Element type. An array can store any kind of elements—from integers to strings to classes. Swift makes it easy to create arrays in your code using an array literal: simply surround a comma-separated list of values with square brackets.
Comments
- Can you show us what you already did?
- Use
Decodable
and declare the struct just with the required keys/members. Pretty easy. - Hey if you want something that let you crate structrue or class for JSON. Please try this out app.quicktype.io
- @TheiOSDev decent tool but i think he needs to understand the codables protocols first so he can handle this by his own in the future, then he can jump into tools that helps him do that faster, any other link related to the codables i think would help him better.
- Doesn't your answer look the same as what already been added ? , plus you don't clarity the error you discussing
- @Sh_Khan Read about
Codable
here, section i believe he can get every explanation possible for each step, + already answered question on SO and i didn't copy pasted your answer if thats what you referring to, i can't tell who posted it first, but if u want i can remove it if it gives you any problems at all - @Sh_Khan and if it was a rare case we would for sure all of us as a community work around it and try to get explanations etc, but its straight forward case that has been posted way too many times, idk even why this question is still not closed, kinda weird no mods online or smth
- @MohmmadS my output looks like
[quizApp.SessionReport.MyObject(status: "true", correct: "1", chapter: "35")]
how to get only array of JSON? - its that array but it prints out the objects like this ^_^ try those commands out , you will get the idea
print(obj.count)// 2 print(obj.first!)
- whats the purpose of
do catch
?