swiftyJSON check if value is null
Asked Answered
C

3

7

I have a json which is array of dictionary

response.text = [{
"id": "4635465675",
"name": "Arts",
"pluralName": "Arts",
"shortName": null
}]

json = JSON((response.text)?.data(using: .utf8)) How can i check is value for key "shortName" null or not, say for first dictionary in the array ? I tried to do like this

if json[0]["shortName"] is NSNull

But it's always true. How can i handle it?

Complacent answered 5/3, 2018 at 9:59 Comment(13)
try this if json["shortName"] is nil . or if let shortName = json["shortName"] { print("shortName - \(shortName)") } else { print("shortName is nil") }Liu
if let _ = json["shortName"] as? String { // it is not null }Chimpanzee
@AshwinShrestha didn't workedComplacent
json[0]["shortName"] is NSNull But it's always true. How can i handle it? off course it will be true because its nullConscious
@Liu didn't worked tooComplacent
Try this. if let name = Json.first?.["shortname"] { }Presbyterian
@Conscious but before it i got IF doesn't it make any difference ?Complacent
@ShauketSheikh nothing :(Complacent
Your code is if json[0]["shortName"] is NSNull { SHORT NAME IS NULL } else { NOT NULL }, its a working codeConscious
@Complacent - Show your full json response and how do you store it into variable jsonLiu
but your json is a dictionary say [String:String], not array of dictionary objects [[String:String]], so i dont know why you are trying to access the first element of json using the index[0]Chimpanzee
@Liu full json [{ "id": "4635465675", "name": "Arts", "pluralName": "Arts", "shortName": null }] and json variable var json = JSON((response.text)?.data(using: .utf8))Complacent
@AshwinShrestha full json [{ "id": "4635465675", "name": "Arts", "pluralName": "Arts", "shortName": null }]Complacent
C
23

you can directly check as the key of JSON.null

if json[0]["shortName"] == JSON.null {
// show the alert

}

if its your String

if json[0]["shortName"].string == nil {
Catheryncatheter answered 5/3, 2018 at 11:11 Comment(3)
YES, that's was exactly what i needed. THANKSComplacent
shouldn't if json[0]["shortName"].stringValue == nil{ change to if json[0]["shortName"].stringValue == ""{ as stringValue always returns String not String?Mover
@WimukthiRajapaksha- thanks for your comment , we can also use like if let getShortName = json[0]["shortName"].string,!getShortName.isEmpty { print(getShortName) }Catheryncatheter
L
0

Execute following code and see

let jsonArray = [{ "id": "4635465675", "name": "Arts", "pluralName": "Arts", "shortName": null }] 

if let jsonObject = jsonArray[0] as? [String : Any] {

    if let id = jsonObject["id"] as? String {
       print("id - \(id)")
    } else {
       print("id does not exist or it is null/nil")
    }

    if let name = jsonObject["name"] as? String {
       print("name - \(name)")
    } else {
       print("name does not exist or it is null/nil")
    }

    if let pluralName = jsonObject["pluralName"] as? String {
       print("pluralName - \(pluralName)")
    } else {
       print("pluralName does not exist or it is null/nil")
    }

    if let shortName = jsonObject["shortName"] as? String {
       print("shortName - \(shortName)")
    } else {
       print("shortName does not exist or it is null/nil - \(jsonObject["shortName"])")
    }


}

Share here result of this code.

Liu answered 5/3, 2018 at 10:21 Comment(3)
Error: initializer for conditiobal binding must have Optional typeComplacent
all lines returned "does not exist or it is null/nil"Complacent
Exact ouput for this line - print("shortName does not exist or it is null/nil - \(jsonObject["shortName"])") ?Liu
C
0

Ok, that's was easier than i was thinking

if json[0]["shortName"].string == nil {
//null
}else{
//not null
}
Complacent answered 5/3, 2018 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.