insert Json file into core Data

I have two json files and want to insert them into core data. Therefor I'd like to convert the json into a dictionary (maybe it is as simple as in python?) and insert the key value pairs one by one into core Data. The json is build as followed:
Code Block
{1 : { "title" : "Mini pork pies with piccalilli", "category" : "meat"}, 2 : {"title" : "apple crumble", "category" : "dessert"}}

I only was able to get the json file and hopefully converted it into a dictionary (as you can see in the code below). Now I do not know what to do next.
Code Block let asset = NSDataAsset(name: "Captions")
        let data = (asset?.data)!
        do{
            let json = try JSONSerialization.jsonObject(with: data, options: [])
            if let object = json as? [Int : Any] {
                print(object)
                return json as! Dictionary<Int, AnyObject>
            }else if let object = json as? [Any] {
                for anItem in object as! [Dictionary<Int, AnyObject>]{
                   print("Not an dictionary, think of something else")
                }
            }



Answered by OOPer in 670601022

The json is build as followed: 

Code Block
{1 : { "title" : "Mini pork pies with piccalilli", "category" : "meat"}, 2 : {"title" : "apple crumble", "category" : "dessert"}}
First of all, {1 : { "title" : "Mini pork pies with piccalilli", "category" : "meat"}, 2 : {"title" : "apple crumble", "category" : "dessert"}} is not a valid JSON. If you want to work with Apple's frameworks, you first need to make a valid JSON.

One possible form:
Code Block
{
"1" : {
"title" : "Mini pork pies with piccalilli",
"category" : "meat"
},
"2" : {
"title" : "apple crumble",
"category" : "dessert"
}
}

(In valid JSON, the keys need to be JSON string, no number keys are allowed.)

Another would be:
Code Block
[
{
"title" : "Mini pork pies with piccalilli",
"category" : "meat"
},
{
"title" : "apple crumble",
"category" : "dessert"
}
]


If you were using the latter form, you would directly get an Array from the JSON. So I assume the first one.
But in any way, if your JSON actually looks like {1 : { "title" : "Mini pork pies with piccalilli", ..., please create a valid JSON first.

Assuming you got the JSON file as my first example, you can write some code to convert it to an Array like this:
Code Block
let jsonText = """
{
"1" : {
"title" : "Mini pork pies with piccalilli",
"category" : "meat"
},
"2" : {
"title" : "apple crumble",
"category" : "dessert"
}
}
"""
func jsonTextToArray(_ jsonText: String) -> [[String: Any]]? {
guard let data = jsonText.data(using: .utf8) else {
print("invalid data")
return nil
}
do {
guard let json = try JSONSerialization.jsonObject(with: data) as? [String: [String: Any]] else {
print("bad format")
return nil
}
return json.sorted {Int($0.key) ?? 0 < Int($1.key) ?? 0}
.map {$0.value}
} catch {
print(error)
return nil
}
}
if let array = jsonTextToArray(jsonText) {
print(array)
} else {
print("bad jsonText")
}
//->[["title": Mini pork pies with piccalilli, "category": meat], ["category": dessert, "title": apple crumble]]


Now I do not know what to do next. 

next… do you mean insert in CoreData ?
If so, you will find a lot of useful information here
https://stackoverflow.com/questions/51869261/save-json-to-coredata-as-string-and-use-the-string-to-create-array-of-objects/51873317
Thank you for your answer, but this is not quite that what I was looking for, yesterday I was a little bit confused what to do next, now I have a plan that do not work.
I want this JSON file (as described above), convert into a dictionary. With this dictionary I'd like to create Arrays, which I insert into CoreData. Inserting into CoreData ist working. I am struggling with convert the JSON file into a Dictionary and extract values from this Dictionary to create an Array.
Maybe there is a simpler way to do it?

I would first convert the list into an array.
let recettes = "{[{\"title\" : \"Mini pork pies with piccalilli\", \"category\" : \"meat\"}, {\"title\" : \"apple crumble\", \"category\" : \"dessert\"}]}"
define a Decodable class .
```
class Receipt: Decodable {
}


Have a look here
https://stackoverflow.com/questions/30480672/how-to-convert-a-json-string-to-a-dictionary
Accepted Answer

The json is build as followed: 

Code Block
{1 : { "title" : "Mini pork pies with piccalilli", "category" : "meat"}, 2 : {"title" : "apple crumble", "category" : "dessert"}}
First of all, {1 : { "title" : "Mini pork pies with piccalilli", "category" : "meat"}, 2 : {"title" : "apple crumble", "category" : "dessert"}} is not a valid JSON. If you want to work with Apple's frameworks, you first need to make a valid JSON.

One possible form:
Code Block
{
"1" : {
"title" : "Mini pork pies with piccalilli",
"category" : "meat"
},
"2" : {
"title" : "apple crumble",
"category" : "dessert"
}
}

(In valid JSON, the keys need to be JSON string, no number keys are allowed.)

Another would be:
Code Block
[
{
"title" : "Mini pork pies with piccalilli",
"category" : "meat"
},
{
"title" : "apple crumble",
"category" : "dessert"
}
]


If you were using the latter form, you would directly get an Array from the JSON. So I assume the first one.
But in any way, if your JSON actually looks like {1 : { "title" : "Mini pork pies with piccalilli", ..., please create a valid JSON first.

Assuming you got the JSON file as my first example, you can write some code to convert it to an Array like this:
Code Block
let jsonText = """
{
"1" : {
"title" : "Mini pork pies with piccalilli",
"category" : "meat"
},
"2" : {
"title" : "apple crumble",
"category" : "dessert"
}
}
"""
func jsonTextToArray(_ jsonText: String) -> [[String: Any]]? {
guard let data = jsonText.data(using: .utf8) else {
print("invalid data")
return nil
}
do {
guard let json = try JSONSerialization.jsonObject(with: data) as? [String: [String: Any]] else {
print("bad format")
return nil
}
return json.sorted {Int($0.key) ?? 0 < Int($1.key) ?? 0}
.map {$0.value}
} catch {
print(error)
return nil
}
}
if let array = jsonTextToArray(jsonText) {
print(array)
} else {
print("bad jsonText")
}
//->[["title": Mini pork pies with piccalilli, "category": meat], ["category": dessert, "title": apple crumble]]


insert Json file into core Data
 
 
Q