Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pretty print JSON string from Data in Swift 4.1 (especially useful printing to Xcode console)
import Foundation
extension Data {
var prettyPrintedJSONString: NSString? { /// NSString gives us a nice sanitized debugDescription
guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return nil }
return prettyPrintedString
}
}
let str = "{\"foo\": \"bar\"}".data(using: .utf8)!.prettyPrintedJSONString!
debugPrint(str)
/* prints:
{
"foo" : "bar"
}
*/
@dongmai
Copy link

dongmai commented Sep 26, 2019

It works well, thanks.

@ivanglushko
Copy link

ivanglushko commented Jan 31, 2020

Thank you works like charm :)

@karlbecker
Copy link

karlbecker commented Mar 5, 2020

Just one more thank you!

@jon-1
Copy link

jon-1 commented Mar 23, 2020

Thanks!

@golfiti
Copy link

golfiti commented Apr 3, 2020

Thanks

@rebeloper
Copy link

rebeloper commented May 28, 2020

Thanks :)

@bybrandt
Copy link

bybrandt commented Jun 3, 2020

Works pretty well, thanks

@gongzhang
Copy link

gongzhang commented Sep 24, 2020

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted

@BadChoice
Copy link

BadChoice commented Nov 3, 2020

Swift 5.1

extension Data {
    var prettyJson: String? {
        guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
              let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
              let prettyPrintedString = String(data: data, encoding:.utf8) else { return nil }

        return prettyPrintedString
    }
}

@rednebmas
Copy link

rednebmas commented Nov 19, 2020

Adding the .sortedKeys option to JSONSerialization.data is nice.

@grigorevp
Copy link

grigorevp commented Dec 17, 2020

Super cool, thanks!

@transat
Copy link

transat commented Feb 2, 2021

Is there a version of this for XML?

@antranapp
Copy link

antranapp commented Feb 18, 2021

Swifty version

extension Encodable {
    var prettyPrintedJSONString: String? {
        let encoder = JSONEncoder()
        encoder.outputFormatting = .prettyPrinted
        guard let data = try? encoder.encode(self) else { return nil }
        return String(data: data, encoding: .utf8) ?? nil
    }
}

@smilingmiao
Copy link

smilingmiao commented Aug 6, 2021

Swift 5.1

extension Data {
    var prettyJson: String? {
        guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
              let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
              let prettyPrintedString = String(data: data, encoding:.utf8) else { return nil }

        return prettyPrintedString
    }
}

This is not ok. we must use NSString not String.

@DevNebulae
Copy link

DevNebulae commented Nov 27, 2021

Using String instead of NSString won't do any harm (documentation).

@cprovatas
Copy link
Author

cprovatas commented Nov 28, 2021

Using String instead of NSString won't do any harm (documentation).

@DevNebulae NSString intentional, note comment: /// NSString gives us a nice sanitized debugDescription

@Booharin
Copy link

Booharin commented Apr 1, 2022

Great!

@sinbadbd
Copy link

sinbadbd commented May 30, 2022

Yes! using NSString its seems to be awesome 👏

@AbduqaxxorSaidjonov
Copy link

AbduqaxxorSaidjonov commented Oct 18, 2022

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment