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

This comment has been minimized.

Copy link

@dongmai dongmai commented Sep 26, 2019

It works well, thanks.

@ivanglushko

This comment has been minimized.

Copy link

@ivanglushko ivanglushko commented Jan 31, 2020

Thank you works like charm :)

@karlbecker

This comment has been minimized.

Copy link

@karlbecker karlbecker commented Mar 5, 2020

Just one more thank you!

@jon-1

This comment has been minimized.

Copy link

@jon-1 jon-1 commented Mar 23, 2020

Thanks!

@golfiti

This comment has been minimized.

Copy link

@golfiti golfiti commented Apr 3, 2020

Thanks

@rebeloper

This comment has been minimized.

Copy link

@rebeloper rebeloper commented May 28, 2020

Thanks :)

@bybrandt

This comment has been minimized.

Copy link

@bybrandt bybrandt commented Jun 3, 2020

Works pretty well, thanks

@gongzhang

This comment has been minimized.

Copy link

@gongzhang gongzhang commented Sep 24, 2020

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

This comment has been minimized.

Copy link

@BadChoice 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

This comment has been minimized.

Copy link

@rednebmas rednebmas commented Nov 19, 2020

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

@grigorevp

This comment has been minimized.

Copy link

@grigorevp grigorevp commented Dec 17, 2020

Super cool, thanks!

@transat

This comment has been minimized.

Copy link

@transat transat commented Feb 2, 2021

Is there a version of this for XML?

@antranapp

This comment has been minimized.

Copy link

@antranapp 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
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment