jsontsv 0.1.6.1 → 0.1.7.0
raw patch · 3 files changed
+23/−5 lines, 3 files
Files
- Main.hs +14/−3
- README.md +8/−1
- jsontsv.cabal +1/−1
Main.hs view
@@ -36,6 +36,7 @@ , nullString :: Text , optTrueString :: Text , optFalseString :: Text+ , optNewLineReplacement :: Char , debugKeyPaths :: Bool } deriving Show @@ -60,6 +61,9 @@ <$> O.strOption (O.value "f" <> O.short 'f' <> O.long "false-string" <> O.metavar "STRING" <> O.help "String to represent boolean false. Default: 'f'"))+ <*> O.option O.auto (O.value ' ' + <> O.short 'N' <> O.long "newline"+ <> O.metavar "STRING" <> O.help "String to replace newlines in field text. Default: ' '") <*> O.switch (O.long "debug" <> O.help "Debug keypaths") parseCSVMode = O.flag' CSVOutput (O.short 'c' <> O.long "csv" <> O.help "Output CSV")@@ -99,7 +103,9 @@ arrayDelim = optArrayDelim , nullValueString = nullString , trueString = optTrueString- , falseString = optFalseString}+ , falseString = optFalseString+ , newlineReplacement = optNewLineReplacement+ } case outputMode of TSVOutput delim -> mapM_ (TL.putStrLn . B.toLazyText . evalToLineBuilder config delim ks') xs CSVOutput -> Prelude.putStrLn . CSV.printCSV $ map (map T.unpack . evalToList config ks') $ xs@@ -125,6 +131,7 @@ , nullValueString :: Text , trueString :: Text , falseString :: Text+ , newlineReplacement :: Char } deriving Show -- | KeyPath may have an alias for the header output@@ -240,7 +247,7 @@ evalKeyPath _ _ _ = Null valToBuilder :: Config -> Value -> B.Builder-valToBuilder _ (String x) = B.fromText x+valToBuilder Config{..} (String x) = B.fromText . T.map (replaceNewLine newlineReplacement) $ x valToBuilder Config{..} Null = B.fromText nullValueString valToBuilder Config{..} (Bool True) = B.fromText trueString valToBuilder Config{..} (Bool False) = B.fromText falseString@@ -251,7 +258,7 @@ valToBuilder _ (Object _) = B.fromText "[Object]" valToText :: Config -> Value -> Text-valToText _ (String x) = x+valToText Config{..} (String x) = T.map (replaceNewLine newlineReplacement) x valToText Config{..} Null = nullValueString valToText Config{..} (Bool True) = trueString valToText Config{..} (Bool False) = falseString@@ -261,3 +268,7 @@ Right int -> T.pack . show $ int valToText _ (Object _) = "[Object]" +replaceNewLine :: Char -> (Char -> Char)+replaceNewLine x s | s `elem` ['\n', '\r'] = x+ | otherwise = s+
README.md view
@@ -140,7 +140,7 @@ Usage: jsontsv FIELDS [-a DELIM] ([-c|--csv] | [-d DELIM]) [-H] [-n|--null-string STRING] [-t|--true-string STRING]- [-f|--false-string STRING] [--debug]+ [-f|--false-string STRING] [-N|--newline STRING] [--debug] Transform JSON objects to TSV. On STDIN provide an input stream of whitespace-separated JSON objects. @@ -154,6 +154,8 @@ -n,--null-string STRING String to represent null value. Default: 'null' -t,--true-string STRING String to represent boolean true. Default: 't' -f,--false-string STRING String to represent boolean false. Default: 'f'+ -N,--newline STRING String to replace newlines in field text. Default: '+ ' --debug Debug keypaths See https://github.com/danchoi/jsontsv for more information.@@ -214,7 +216,11 @@ * If the leaf value is an array, it is concatenated into a single comma-separated string. This delimiter can be changed with the `-a` option. +## Newlines in content +If a string field in the JSON contains a `\n` or `\r` character, these will be replaced +by spaces by default. The replacement character can be changed with the `-N` option.+ ## Column header aliases If the default column headers using `-H` are too long, you can designate@@ -223,6 +229,7 @@ curl -s "https://api.github.com/repos/rails/rails/issues" | jq -M '.[]' | jsontsv -H 'number title user.login:login state repository.name:repo_name' + ## Known alternatives
jsontsv.cabal view
@@ -2,7 +2,7 @@ -- see http://haskell.org/cabal/users-guide/ name: jsontsv-version: 0.1.6.1+version: 0.1.7.0 synopsis: JSON to TSV transformer homepage: https://github.com/danchoi/jsontsv description: Transforms JSON into tab-separated line-oriented output, for easier processing in Unix-style pipelines.