Querying JSON to return a value based on date

Dear all,

I would like to ask if there is a way to do a query off a JSON file? Currently, I am trying to create a simple quote of the day app. What I have in my JSON file is the following:

[
{
“QDate”:“Aug 17”,
“Quote”: “Quote for Aug 17.”
},
{
“QDate”:“Aug 18”,
“Quote”: “Quote for Aug 18.”
}
]

So far, I have used date formatter which returns date in format as shown in QDate. Now, is there a way to use this piece of info to output the quote?

Based on the recipes app on the course, I can parse the JSON file properly… Now I am exploring whether I can perform a query-like function as well… Hope I can get some pointers on what are possible ways I can do this. Thank you for your kind attention.

Best,
Boon

Assuming you parse the JSON into an array of structs/classes, you can use first(where:) or filter(_:) on the array to match against whatever date you are querying.

Roughly speaking:

let arrayOfQuotes = //build your array from JSON
let quoteDate = //get your date from wherever
if let quote = arrayOfQuotes.first(where: { $0.QDate == quoteDate }) {
    //do whatever with your quote
}

Oh thank you for your prompt reply! Let me give that a try :pray: @roosterboy

Thanks again @roosterboy . The code works beautifully