karatelabs/karate · error · RuntimeException

fromJson() requires a JSON string argument

Error message

fromJson() requires a JSON string argument

What it means

MarkupContext exposes a JS-callable `fromJson(text)` that parses a JSON string into an object. When called with no arguments or a null first argument, the JavaInvokable wrapper throws this RuntimeException, because there is no JSON text to parse.

Solutions

  1. Pass an actual JSON string: `context.fromJson('{"a":1}')`.
  2. Guard optional input: only call fromJson when the string is non-null, or default it to '{}' / 'null' as appropriate.
  3. Check where the JSON string originates (model attribute, read() of a file) and ensure it is loaded before parsing.
  4. If input may be empty-string or malformed, wrap in try-catch — this specific error only covers missing/null arguments.

Example fix

// before
var data = context.fromJson(jsonBlob);

// after
var data = jsonBlob != null ? context.fromJson(jsonBlob) : {};
Defensive patterns

Strategy: validation

Validate before calling

function safeFromJson(ctx, s) {
  if (s == null || s === '') return null;
  return ctx.fromJson(s);
}

Type guard

function isJsonString(s) { return typeof s === 'string' && s.trim().length > 0; }
// usage: if (isJsonString(s)) context.fromJson(s);

Try / catch

try {
  var obj = context.fromJson(s);
} catch (e) {
  if (e.message === 'fromJson() requires a JSON string argument') {
    obj = null;
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling `context.fromJson()` or `context.fromJson(null)` in template script — argument omitted or bound to a null variable (e.g. reading an optional JSON config blob that is unset).

Common situations: Optional JSON payloads from the model that are null when not provided; forgetting the string argument; passing a non-string indirectly — note that a non-null argument is stringified via toString before parsing, so only missing/null args hit this error.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/c46b48fcf32a0bf8. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/markup/MarkupContext.java:145

                    throw new RuntimeException("read() requires a path argument");
                }
                return read(args[0].toString());
            };
            case "readBytes" -> (JavaInvokable) args -> {
                if (args.length == 0 || args[0] == null) {
                    throw new RuntimeException("readBytes() requires a path argument");
                }
                return readBytes(args[0].toString());
            };
            case "toJson" -> (JavaInvokable) args -> {
                // null IS a valid JSON-able value (`null`), so only the
                // missing-arg case errors here.
                if (args.length == 0) throw new RuntimeException("toJson() requires an object argument");
                return toJson(args[0]);
            };
            case "fromJson" -> (JavaInvokable) args -> {
                if (args.length == 0 || args[0] == null) {
                    throw new RuntimeException("fromJson() requires a JSON string argument");
                }
                return fromJson(args[0].toString());
            };
            // context.get(name, default?) for optional fragment params.
            // Walks the current eval scope (`_` map first, then wrapped
            // Thymeleaf scope). Returns the bound non-null value if found,
            // else the default (or null when no default is given).
            case "get" -> (JavaInvokable) args -> {
                if (args.length == 0 || args[0] == null) {
                    throw new RuntimeException("context.get() requires a name argument");
                }
                String name = args[0].toString();
                Object defaultValue = args.length > 1 ? args[1] : null;
                MarkupScope scope = getMarkupScope();
                if (scope != null) {
                    Object v = scope.lookup(name);
                    if (v != null) return v;
                }

View on GitHub (pinned to a22eb90246)