Crash when adding large JSON data to SwiftData: Could not cast value of type '__NSCFBoolean' to 'NSString'

Within the app main struct, the SwiftData modelContainer is created by fetching and decoding JSON from remote site. The decoded data is inserted into the modelContext. Immediately thereafter, the app crashes with the error Could not cast value of type '__NSCFBoolean' to 'NSString' . I have run the JSON from the site through JSON lint and it is correctly structured.

The JSON data is structured as follows: Overview object which includes a Result object which includes a List Array. Each List object includes a Books Array.

I have run the app through AI and the result indicates that there is data corruption when adding the data to the database. The AI rewrote the code such that the book data is encoded into a data blob and then adds the data to the database, instead of adding the individual books (which is extensive.) This approach works without crashing, but I would like to find out if this is indeed a SwiftData issue.

Extremely difficult to determine the problem when you don't supply any code showing how you're reading the JSON data into your structs, and no JSON data showing the actual data you're reading.

Yeah, if you can provide a minimal project that contains only the code and data needed to reproduce the issue, folks here may be able to help more effectively. Your post can contain a link to where your test project is hosted.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Here is a GitHub link with a minimalist project that exhibits the crash after startup.

https://github.com/frios/BestSellerList

I have commented out the startup code under .modelContainer but the same thing happens in the ContentView.task code, just takes a few seconds longer.

I have additional information on this issue. When I look at the raw JSON data as received from the API call, all of the data is present, but when I look at the data after I decode it, several fields have empty strings which did have string data in the original response. In particular, display_name, list_name and list_name_encoded all have empty strings. Why would the decoding fail on simple strings and set the value to the default ""?

I have uploaded JSONData.json and RawData.JSON to the above GitHub project show the differences.

Crash when adding large JSON data to SwiftData: Could not cast value of type '__NSCFBoolean' to 'NSString'
 
 
Q