Last active
February 11, 2017 05:29
-
-
Save kristopherjohnson/c50f79fb71046825c056e5d2ce088e34 to your computer and use it in GitHub Desktop.
Reads JSON from standard input and writes pretty-printed JSON to standard output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/swift | |
| import Cocoa | |
| let stdin = FileHandle.standardInput | |
| let stdout = FileHandle.standardOutput | |
| let stderr = FileHandle.standardError | |
| do { | |
| let inputData = stdin.readDataToEndOfFile() | |
| let jsonObject = try JSONSerialization.jsonObject(with: inputData, | |
| options: []) | |
| let outputData = try JSONSerialization.data(withJSONObject: jsonObject, | |
| options: [.prettyPrinted]) | |
| stdout.write(outputData) | |
| print() | |
| } | |
| catch let error as NSError { | |
| let message = "error: \(error.localizedDescription)\n" | |
| stderr.write(message.data(using: .utf8)!) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Save this as a file, do
chmod +x prettyjson.swiftto make it executable, then you can use it like this: