Fix json parser (#80)

* fix #77: parse floats and nils as well and convert them to string
This commit is contained in:
T.v.Dein
2025-10-06 22:54:31 +02:00
committed by GitHub
parent 8c87da34f2
commit 35b726fee4
3 changed files with 62 additions and 1 deletions

View File

@@ -28,7 +28,7 @@ import (
) )
const DefaultSeparator string = `(\s\s+|\t)` const DefaultSeparator string = `(\s\s+|\t)`
const Version string = "v1.5.7" const Version string = "v1.5.8"
const MAXPARTS = 2 const MAXPARTS = 2
var DefaultConfigfile = os.Getenv("HOME") + "/.config/tablizer/config" var DefaultConfigfile = os.Getenv("HOME") + "/.config/tablizer/config"

View File

@@ -25,6 +25,7 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"math"
"regexp" "regexp"
"strings" "strings"
@@ -222,6 +223,32 @@ func parseRawJSON(conf cfg.Config, input io.Reader) (Tabdata, error) {
row[idxmap[currentfield]] = val row[idxmap[currentfield]] = val
} }
} }
case float64:
var value string
// we set precision to 0 if the float is a whole number
if val == math.Trunc(val) {
value = fmt.Sprintf("%.f", val)
} else {
value = fmt.Sprintf("%f", val)
}
if !haveheaders {
row = append(row, value)
} else {
row[idxmap[currentfield]] = value
}
case nil:
// we ignore here if a value shall be an int or a string,
// because tablizer only works with strings anyway
if !haveheaders {
row = append(row, "")
} else {
row[idxmap[currentfield]] = ""
}
case json.Delim: case json.Delim:
if val.String() == "}" { if val.String() == "}" {
data = append(data, row) data = append(data, row)
@@ -240,6 +267,8 @@ func parseRawJSON(conf cfg.Config, input io.Reader) (Tabdata, error) {
haveheaders = true haveheaders = true
} }
isjson = true isjson = true
default:
fmt.Printf("unknown token: %v type: %T\n", t, t)
} }
iskey = !iskey iskey = !iskey

View File

@@ -180,6 +180,38 @@ func TestParserJSONInput(t *testing.T) {
expect: Tabdata{}, expect: Tabdata{},
}, },
{
// contains nil, int and float values
name: "niljson",
wanterror: false,
input: `[
{
"NAME": "postgres-operator-7f4c7c8485-ntlns",
"READY": "1/1",
"STATUS": "Running",
"RESTARTS": 0,
"AGE": null,
"X": 12,
"Y": 34.222
}
]`,
expect: Tabdata{
columns: 7,
headers: []string{"NAME", "READY", "STATUS", "RESTARTS", "AGE", "X", "Y"},
entries: [][]string{
[]string{
"postgres-operator-7f4c7c8485-ntlns",
"1/1",
"Running",
"0",
"",
"12",
"34.222000",
},
},
},
},
{ {
// one field missing + different order // one field missing + different order
// but shall not fail // but shall not fail