ParseJSONMap parses a JSON string into a map. Returns an empty (never nil) map for empty input or the JSON literal null, so callers can always overlay onto the result without a nil-map panic. Supports stdin (-), @file, @@-escape, and single-quote stripping via ResolveInput.
(input, label string, stdin io.Reader, fileIO fileio.FileIO)
| 43 | // the result without a nil-map panic. |
| 44 | // Supports stdin (-), @file, @@-escape, and single-quote stripping via ResolveInput. |
| 45 | func ParseJSONMap(input, label string, stdin io.Reader, fileIO fileio.FileIO) (map[string]any, error) { |
| 46 | resolved, err := ResolveInput(input, stdin, fileIO) |
| 47 | if err != nil { |
| 48 | return nil, errs.NewValidationError(errs.SubtypeInvalidArgument, "%s: %s", label, err). |
| 49 | WithParam(label). |
| 50 | WithCause(err) |
| 51 | } |
| 52 | if resolved == "" { |
| 53 | return map[string]any{}, nil |
| 54 | } |
| 55 | var result map[string]any |
| 56 | if err := json.Unmarshal([]byte(resolved), &result); err != nil { |
| 57 | return nil, errs.NewValidationError(errs.SubtypeInvalidArgument, "%s invalid format, expected JSON object", label). |
| 58 | WithParam(label). |
| 59 | WithCause(err) |
| 60 | } |
| 61 | if result == nil { |
| 62 | // `null` unmarshals into a nil map without error; normalize it so the |
| 63 | // returned map is always writable, matching the empty-input case. |
| 64 | return map[string]any{}, nil |
| 65 | } |
| 66 | return result, nil |
| 67 | } |