Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test if a property exists on a JSON object? #1119

Open
RTSMikeM opened this issue Jul 25, 2022 · 1 comment
Open

How to test if a property exists on a JSON object? #1119

RTSMikeM opened this issue Jul 25, 2022 · 1 comment

Comments

@RTSMikeM
Copy link

Xcode 13.4.1 with SwiftUI

Given a simple JSON object (hash)
How can I check if an optional property exists?

Ex: JSON object is of type SwiftyJSON:

if(JSON.exists("myOptionalProperty") {
//DO SOMETHING
}

Either I'm missing this or it's a lacking feature.

@luoxiu
Copy link
Contributor

luoxiu commented Nov 4, 2022

let string = """
{
  "foo": null
}
"""

let json = JSON(parseJSON: string)

// check if the key exists
json["foo"].exists() // true

// check if the key is null
json["bar"].type == .null // true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants