UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Datamodel to Parse NASA JSON

Forums > Swift

Help! :-) I'm trying to create a datamodel that I can use to obtain: data[ nasa_id title description date_created]

and "href" (for loading an image) from links[href] from the json here: https://images-api.nasa.gov/search?media_type=image

I can get a response from the site but no data in my datamodel. I think it has to do with the "arays in arrays" but I can't wrap my head around it.

Any thoughts? (I'm using swiftui but the datamodel class is just plain swift)

3      

@twostraws has a number of videos about pulling JSON data from the web.

In November he posted a nice video about pulling data from a NASA web site!

Did you review this? Working with Hierarchical Codable Data

Review his videos a few times and get the code working! Then tell us specifically what you don't understand about Codable data, Hierarchical JSON, etc.

We're here to help! But we're probably not here to restate what @twostraws has already published!

3      

Hello,

I think the issue here is that the API you shared provides link (the href attribute) to another JSON result which has the actual images.

So you will need to do new request for each href to get the actual image data available as array of strings (example)

To get started with wrapping your head around the structure, you can use on-line tools that will convert JSON into Codable models in Swift for you. Although they usually don't do great job, so it will require some cleanup.

4      

Hi the href is a link to where the image is stored not a JSON. This is a great place to use the AsyncImage(url: )

How to load a remote image from a URL.

I use a JSON Modeler called Ducky it free to download.

Stewart Lynch Video How to use Ducky

4      

Thanks for the helpful responses. Let me ask this another way: Given the attached json response, what would a good data model look like to get at the values (as seen below)?

Thanks in advance!

https://drive.google.com/file/d/1dkxLTpV7J3MXyWpn4A5f15aID0RnQnRC/view?usp=sharing

3      

@guitarcoey asks:

Given the attached json response, what would a good data model look like to get at the values (as seen below)?

Someone here could just give you a model and you'd be on your way. Or, we can help you learn how to derive models from JSON so you can do this on your own in the future. Better yet, in the future you can help another HackingWithSwift programmer with their JSON related questions. The HackingWithSwift way leans towards the second way.

In fact, @Nigel answered your question before you asked it!

The first line in Stewart Lynch's video synopsis on YouTube says:

Ducky is a document-based app that helps you infer models from JSON.

No one wants to slog through lines of JSON code and manually tinker with structures to derive a workable data model to use in their Swift application. Use the tool!

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.