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

@@ -25,6 +25,7 @@ import (
"fmt"
"io"
"log"
"math"
"regexp"
"strings"
@@ -222,6 +223,32 @@ func parseRawJSON(conf cfg.Config, input io.Reader) (Tabdata, error) {
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:
if val.String() == "}" {
data = append(data, row)
@@ -240,6 +267,8 @@ func parseRawJSON(conf cfg.Config, input io.Reader) (Tabdata, error) {
haveheaders = true
}
isjson = true
default:
fmt.Printf("unknown token: %v type: %T\n", t, t)
}
iskey = !iskey