Load JSON in SwiftUI with arbitrary keys - json

I have a pretty simple JSON file I would like to load into a SwiftUI project. The JSON file looks like this:
{
"all": ["foo", "bar", "baz", ...],
"4": ["asd", "qwe", ...],
"25": ["something", "another", ...],
...
"2": ["xxx", "yyy", ...]
}
Pretty simple, a dictionary where each value is a list of strings. The keys are "all" and then an arbitrary number of integers such as "1", "123" and so on. They must not be in order, they can be any number, and there may be any number of them. So I don't know before hand how many keys there are.
I have managed to get this JSON into a variable of type String. But now I am stuck with how to parse this so I can for example fetch the list at key "all", or check if the key "123" exists and if so get that list.
How do I do that in SwiftUI?
Oh, and by the way, the string are Unicode and contain special characters like åäö.

try this approach:
struct ContentView: View {
#State var dataList: [String:[String]] = [:]
var body: some View {
List(Array(dataList.keys), id: \.self) { key in
Section(header: Text(key).foregroundColor(.red).font(.headline)) {
if let values = dataList[key] {
ForEach(values, id: \.self) { item in
Text("\(item)")
}
}
}
}
.onAppear {
let json = """
{
"all": ["foo", "bar", "baz"],
"4": ["asd", "qwe"],
"25": ["something", "another"],
"2": ["xxx", "yyy", "åäö"],
"åäö": ["qwerty", "uiop", "dfghjkh"]
}
"""
if let data = json.data(using: .utf8) {
do {
self.dataList = try JSONDecoder().decode([String: [String]].self, from: data)
} catch {
print("decode error: \(error)")
}
}
}
}
}

Related

JSON parsing error - No value associated with key CodingKeys

So, I have tried looking for the answer, I have seen many questions similar to mine. I have tried adding an enum codingKey, I have tried reworking the JSON, but nothing seems to work. I know it's probably something very simple too. (I'm a noob.) Please help.
I am making a Quotes app as part of a project for a course I'm taking.
Here's the code for the Model:
import Foundation
class AllQuotes: Identifiable, Decodable {
var id:UUID?
var quoteTopic:String
var topicImage:String
var featured:Bool
var QuotesList:[Quotes]
}
class Quotes: Identifiable, Decodable {
var id:UUID?
var name:String
var actualQuote:String
var image:String?
private enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case actualQuote = "actualQuote"
case image = "image"
}
}
Here's my JSON code:
[
{
"quoteTopic": "Wise Quotes",
"topicImage": "wise quotes",
"featured": false,
"QuotesList": [
{
"name": "Lao Tzu",
"actualQuote": "The journey of a thousand miles begins with one step.",
"image": null
},
{
"name": "Mark Twain",
"actualQuote": "It is better to keep your mouth closed and let people think you are a fool than to open it and remove all doubt.",
"image": null
},
{
"name": "Mark Twain",
"actualQuote": "The secret of getting ahead is getting started.",
"image": null
},
{
"name": "Babe Ruth",
"actualQuote": "It’s hard to beat a person who never gives up.",
"image": null
}
]
},
{
"quoteTopic": "Motivational Quotes",
"topicImage": "motivational quotes",
"featured": true,
"QuotesList": [
{
"name": "Mark Twain",
"actualQuote": "Age is an issue of mind over matter. If you don't mind, it doesn't matter.",
"image": null
},
{
"name": "Mahatma Gandhi",
"actualQuote": "Learn as if you will live forever, live like you will die tomorrow.",
"image": null
},
{
"name": "Mary Kay Ash",
"actualQuote": "Don’t limit yourself. Many people limit themselves to what they think they can do. You can go as far as your mind lets you. What you believe, remember, you can achieve.",
"image": null
},
{
"name": "Unknown",
"actualQuote": "Hold the vision, trust the process.",
"image": null
}
]
},
{
"quoteTopic": "Success Quotes",
"topicImage": "success quotes",
"featured": false,
"QuotesList": [
{
"name": "Estee Lauder",
"actualQuote": "I never dreamed about success. I worked for it.",
"image": null
},
{
"name": "Thomas Edison",
"actualQuote": "Opportunity is missed by most people because it is dressed in overalls and looks like work.",
"image": null
},
{
"name": "Tom Lehrer",
"actualQuote": "Life is like a sewer… what you get out of it depends on what you put into it.",
"image": null
},
{
"name": "Walt Disney",
"actualQuote": "All our dreams can come true, if we have the courage to pursue them.",
"image": null
}
]
}
]
and here's the error I'm getting
Couldn't decode json, try again (to get the actual quote)!
keyNotFound(CodingKeys(stringValue: "name", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: "name", intValue: nil) ("name").", underlyingError: nil))
(NOTE: It RUNS/builds fine, but it just won't show the 'QuotesDetailView' page when I try to run it. It's SwiftUI.
Please let me know if I need to provide anymore information, thank you!
EDIT: here's how I decode my JSON
class DataService {
// Return an array of Quote Objects
static func getLocalData() -> [AllQuotes] {
// Begin the process of parsing the JSON File
// Get a URL path to json file
let pathString = Bundle.main.path(forResource: "quotes", ofType: "json")
// Check if pathString is nil, otherwise return empty Quotes List if it is.
guard pathString != nil else{
return [AllQuotes]()
}
// Create URL Object
let url = URL(fileURLWithPath: pathString!)
// Create Data Object
do {
let data = try Data(contentsOf: url)
// Parse the data
let decoder = JSONDecoder()
do {
let quoteData = try decoder.decode([AllQuotes].self, from: data)
// Set unique IDs for each instance
for newQuote in quoteData {
newQuote.id = UUID()
}
// Return the Quote
return quoteData
} catch {
// Couldn't decode json
print("Couldn't decode json, try again (to get the quotes TOPIC)!")
print(error)
}
} catch {
// Error fetching data from file
print("There was an error fetching the data from the file. - with the quote list!")
print(error)
}
// It didn't work, return an empty Quotes List
return [AllQuotes]()
}
// Return an array of ACTUAL Quotes Objects
static func getActualQuote() -> [Quotes] {
// Begin the process of parsing the JSON File
// Get a URL path to json file
let pathString = Bundle.main.path(forResource: "quotes", ofType: "json")
// Check if pathString is nil, otherwise return empty Quotes List if it is.
guard pathString != nil else{
return [Quotes]()
}
// Create URL Object
let url = URL(fileURLWithPath: pathString!)
// Create Data Object
do {
let data = try Data(contentsOf: url)
// Parse the data
let decoder = JSONDecoder()
do {
let actualQuoteData = try decoder.decode([Quotes].self, from: data)
// Set unique IDs for each instance
for actualQuote in actualQuoteData {
actualQuote.id = UUID()
}
// Return the Quote
return actualQuoteData
} catch {
// Couldn't decode json
print("Couldn't decode json, try again (to get the actual quote)!")
print(error)
}
} catch {
// Error fetching data from file
print("There was an error fetching the data from the file. - with the actual quote!")
print(error)
}
// It didn't work, return an empty Quotes List
return [Quotes]()
}
}
try this sample code, it shows how to decode your json data, and then display some info:
Since you don't show how you decode your json data, I'm gessing that
the error you get is due to decoding AllQuotes.self instead of [AllQuotes].self as required.
struct ContentView: View {
#State var quoteList = [Quotes]()
var body: some View {
List(quoteList) { quote in
Text(quote.name)
}
.onAppear {
let json = """
[
{
"quoteTopic": "Wise Quotes",
"topicImage": "wise quotes",
"featured": false,
"QuotesList": [
{
"name": "Lao Tzu",
"actualQuote": "The journey of a thousand miles begins with one step.",
"image": null
},
{
"name": "Mark Twain",
"actualQuote": "It is better to keep your mouth closed and let people think you are a fool than to open it and remove all doubt.",
"image": null
},
{
"name": "Mark Twain",
"actualQuote": "The secret of getting ahead is getting started.",
"image": null
},
{
"name": "Babe Ruth",
"actualQuote": "It’s hard to beat a person who never gives up.",
"image": null
}
]
},
{
"quoteTopic": "Motivational Quotes",
"topicImage": "motivational quotes",
"featured": true,
"QuotesList": [
{
"name": "Mark Twain",
"actualQuote": "Age is an issue of mind over matter. If you don't mind, it doesn't matter.",
"image": null
},
{
"name": "Mahatma Gandhi",
"actualQuote": "Learn as if you will live forever, live like you will die tomorrow.",
"image": null
},
{
"name": "Mary Kay Ash",
"actualQuote": "Don’t limit yourself. Many people limit themselves to what they think they can do. You can go as far as your mind lets you. What you believe, remember, you can achieve.",
"image": null
},
{
"name": "Unknown",
"actualQuote": "Hold the vision, trust the process.",
"image": null
}
]
},
{
"quoteTopic": "Success Quotes",
"topicImage": "success quotes",
"featured": false,
"QuotesList": [
{
"name": "Estee Lauder",
"actualQuote": "I never dreamed about success. I worked for it.",
"image": null
},
{
"name": "Thomas Edison",
"actualQuote": "Opportunity is missed by most people because it is dressed in overalls and looks like work.",
"image": null
},
{
"name": "Tom Lehrer",
"actualQuote": "Life is like a sewer… what you get out of it depends on what you put into it.",
"image": null
},
{
"name": "Walt Disney",
"actualQuote": "All our dreams can come true, if we have the courage to pursue them.",
"image": null
}
]
}
]
"""
if let data = json.data(using: .utf8) {
do {
let apiResponse = try JSONDecoder().decode([AllQuotes].self, from: data)
// print something
for quote in apiResponse {
print("---> quoteTopic: \(quote.quoteTopic)")
// all quotes
quoteList.append(contentsOf: quote.QuotesList)
}
} catch {
print("decode error: \(error)")
}
}
}
}
}
class AllQuotes: Identifiable, Decodable {
let id = UUID() // <-- here
var quoteTopic:String
var topicImage:String
var featured:Bool
var QuotesList:[Quotes]
// -- here
private enum CodingKeys: String, CodingKey {
// <-- here remove id
case quoteTopic, topicImage, featured, QuotesList
}
}
class Quotes: Identifiable, Decodable {
let id = UUID() // <-- here
var name:String
var actualQuote:String
var image:String?
// -- here
private enum CodingKeys: String, CodingKey {
// <-- here remove id
case name = "name"
case actualQuote = "actualQuote"
case image = "image"
}
}
EDIT-1: in view of the "new" code
struct ContentView: View {
#State var quoteList = [Quotes]()
var body: some View {
List(quoteList) { quote in
Text(quote.name)
}
.onAppear {
quoteList = DataService.getActualQuote()
print("---> quoteList: \(quoteList)")
}
}
}
class DataService {
// Return an array of Quote Objects
static func getLocalData() -> [AllQuotes] {
// Begin the process of parsing the JSON File
// Get a URL path to json file
let pathString = Bundle.main.path(forResource: "quotes", ofType: "json")
// Check if pathString is nil, otherwise return empty Quotes List if it is.
guard pathString != nil else{
return [AllQuotes]()
}
let url = URL(fileURLWithPath: pathString!)
do {
let data = try Data(contentsOf: url)
do {
let quoteData = try JSONDecoder().decode([AllQuotes].self, from: data)
return quoteData
} catch {
// Couldn't decode json
print("Couldn't decode json, try again (to get the quotes TOPIC)!")
print(error)
}
} catch {
// Error fetching data from file
print("There was an error fetching the data from the file. - with the quote list!")
print(error)
}
// It didn't work, return an empty Quotes List
return []
}
// Return an array of ACTUAL Quotes Objects
static func getActualQuote() -> [Quotes] {
// Begin the process of parsing the JSON File
// Get a URL path to json file
let pathString = Bundle.main.path(forResource: "quotes", ofType: "json")
// Check if pathString is nil, otherwise return empty Quotes List if it is.
guard pathString != nil else{
return [Quotes]()
}
// Create URL Object
let url = URL(fileURLWithPath: pathString!)
// Create Data Object
do {
let data = try Data(contentsOf: url)
do {
// -- here
let quoteData = try JSONDecoder().decode([AllQuotes].self, from: data)
// -- here
var actualQuoteData = [Quotes]()
for quote in quoteData {
actualQuoteData.append(contentsOf: quote.QuotesList)
}
// Return the Quotes
return actualQuoteData
} catch {
// Couldn't decode json
print("Couldn't decode json, try again (to get the actual quote)!")
print(error)
}
} catch {
// Error fetching data from file
print("There was an error fetching the data from the file. - with the actual quote!")
print(error)
}
// It didn't work, return an empty Quotes List
return []
}
}
EDIT-2: you can shorten your code, such as:
class DataService {
// Return an array of Quote Objects
static func getLocalData() -> [AllQuotes] {
if let pathString = Bundle.main.path(forResource: "quotes", ofType: "json") {
let url = URL(fileURLWithPath: pathString)
do {
let data = try Data(contentsOf: url)
let quoteData = try JSONDecoder().decode([AllQuotes].self, from: data)
return quoteData
} catch {
print(error)
}
}
return []
}
// Return an array of ACTUAL Quotes Objects
static func getActualQuote() -> [Quotes] {
var actualQuoteData = [Quotes]()
for quote in getLocalData() {
actualQuoteData.append(contentsOf: quote.QuotesList)
}
return actualQuoteData
}
}

Swift decode JSON with unknown keys

I have a JSON file in my app bundle that looks like this
{
"1": "cat",
"2": "dog",
"3": "elephant"
}
What I'd like is to be able to find the value for the "2" key for example ("dog").
I'm using this extension to decode the json file:
let config = Bundle.main.decode(Config.self, from: "config.json")
And I have this struct defined:
struct Config: Codable {
let id: String
let animal: String
}
But how do I find the animal name for the "2" key?
You seem to be trying to decode your JSON as if it were an array of your Config structs - that would look like this:
[
{
"id": "1",
"animal": "cat"
},
{
"id": "2",
"animal": "dog"
},
{
"id": "3",
"animal": "elephant"
}
]
But your data (config.json) isn't that, it's just a JSON dictionary of String keys with String values.
You can instead "decode" it as just a String: String dictionary like:
let dict = Bundle.main.decode([String: String].self, from: "config.json")
and then dict["2"] would indeed be an optional string, with a value of .some("dog")
Or perhaps you mean your JSON to be an array of Config's, if you change the contents of config.json file to the above, and then decode it with:
let config = Bundle.main.decode([Config].self, from: "config.json")
Then the animal with id of 2 would be, e.g.
config.first(where: { $0.id == "2" })?.animal

Casting dictionary of sets to JSON Object

I'm trying to build request body like this form:
{
"user": {
"id": 1,
"id": 2,
"id": 4
}
}
My first idea was built json from string and cast it to dictionary String and Any, but this solution have a issue. Some "id" are missing on casting by JSONSerialization step.
I tried to use:
var dictionary: [String : Any] = ["name" : "John"]()
var selectedIDs = Set<NSDictionary>()
// Adding values to selectedIDs set
let userIDDict = ["id" : id] as NSDictionary
selectedIDs.insert(userIDDict)
dictionary.updateValue(selectedIDs, forKey: "user")
But it cannot be cast by JSONSerialization (Invalid type in JSON write).
How can i resolve problem, which i'm facing?
Creating this request is not a problem; it's just not proper JSON, so you shouldn't try to use JSONSerialization. It's just a string, and so you can create that string:
let idKeyValues = ids.map { "\"id\": \($0)" }.joined(separator: ",\n ")
let request = """
{
"user": {
\(idKeyValues)
}
}
"""
===>
{
"user": {
"id": 1,
"id": 2,
"id": 4
}
}
The proper way to express this in JSON would be:
{
"user": {
"ids": [1, 2, 4]
}
}
With that, a Codable implementation should be very straightforward.

Swift 4 codable: the keys are Int array in JSON data

{
"0": {
"name": "legaldoc.pdf",
"cmisid": "yib5C-w92PPtxTBlXl4UJ8oDBthDtAU9mKN5kh2_KrQ"
},
"1": {
"name": "persdoc.pdf",
"cmisid": "dqAnrdNMXGTz1RbOMI37OY6tH9xMdxiTnz6wEl2m-VE"
},
"2": {
"name": "certdoc.pdf",
"cmisid": "6d7DuhldQlnb0JSjXlZb9mMOjxV3E_ID-ynJ0QRPMOA"
}
}
How do I use Swift 4 Codable to parse JSON data like that? the problem is that the keys are Int array.
How do I set CodingKeys for this?
As mentioned in the comments there is no array. All collection types are dictionaries.
You can decode it as Swift dictionary. To get an array map the result to the values of the sorted keys
let jsonString = """
{
"0": {
"name": "legaldoc.pdf",
"cmisid": "yib5C-w92PPtxTBlXl4UJ8oDBthDtAU9mKN5kh2_KrQ"
},
"1": {
"name": "persdoc.pdf",
"cmisid": "dqAnrdNMXGTz1RbOMI37OY6tH9xMdxiTnz6wEl2m-VE"
},
"2": {
"name": "certdoc.pdf",
"cmisid": "6d7DuhldQlnb0JSjXlZb9mMOjxV3E_ID-ynJ0QRPMOA"
}
}
"""
struct Item : Codable {
let name, cmisid : String
}
do {
let data = Data(jsonString.utf8)
let result = try JSONDecoder().decode([String: Item].self, from: data)
let keys = result.keys.sorted()
let array = keys.map{ result[$0]! }
print(array)
} catch {
print(error)
}

How to parse JSON in Swift?

I have some JSON data that looks like this which I am trying to parse in Swift.
[
[
{
a: "1",
b: "2"
},
[
{
c: "3",
},
{
d: "4",
}
]
]
]
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
if let myArray = json[0] as? [[AnyObject]] {
for myObject in myArray {
print("This works!\(myObject)")
}
}
However nothing I try seems to work - any help would be appreciated.
you can use SwiftyJSON - https://github.com/SwiftyJSON/SwiftyJSON
or create a class based on your JSON scheme try to parse with it.
like:
class object
{
let data = Array<subObject>()
}
class subObject
{
let subData = Array<Dictionary<AnyObject,AnyObject>>()
}
This snippet is not JSON. If it was JSON, the keys would be strings, like this:
[
[
{
"a": "1",
"b": "2"
},
[
{
"c": "3",
},
{
"d": "4",
}
]
]
]
And anyway in your screenshot we see that your JSON has already been parsed!
What you show in the image is not JSON either, but an array containing arrays and dictionaries...
But let's say your JSON is actually valid and the missing quotes are just a copy/paste problem.
Then to achieve your goal you have to cast the result of NSJSONSerialization to the correct JSON format, then you can access the inner objects.
Like this, for example:
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [[AnyObject]] {
if let myArray = json.first {
for myObject in myArray {
print("This works!\(myObject)")
}
}
}
} catch let error as NSError {
print(error.localizedDescription)
}