{"record":{"id":"c46b48fcf32a0bf8","repo":"karatelabs/karate","slug":"fromjson-requires-a-json-string-argument","errorCode":null,"errorMessage":"fromJson() requires a JSON string argument","messagePattern":"fromJson\\(\\) requires a JSON string argument","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/markup/MarkupContext.java","lineNumber":145,"sourceCode":"                    throw new RuntimeException(\"read() requires a path argument\");\n                }\n                return read(args[0].toString());\n            };\n            case \"readBytes\" -> (JavaInvokable) args -> {\n                if (args.length == 0 || args[0] == null) {\n                    throw new RuntimeException(\"readBytes() requires a path argument\");\n                }\n                return readBytes(args[0].toString());\n            };\n            case \"toJson\" -> (JavaInvokable) args -> {\n                // null IS a valid JSON-able value (`null`), so only the\n                // missing-arg case errors here.\n                if (args.length == 0) throw new RuntimeException(\"toJson() requires an object argument\");\n                return toJson(args[0]);\n            };\n            case \"fromJson\" -> (JavaInvokable) args -> {\n                if (args.length == 0 || args[0] == null) {\n                    throw new RuntimeException(\"fromJson() requires a JSON string argument\");\n                }\n                return fromJson(args[0].toString());\n            };\n            // context.get(name, default?) for optional fragment params.\n            // Walks the current eval scope (`_` map first, then wrapped\n            // Thymeleaf scope). Returns the bound non-null value if found,\n            // else the default (or null when no default is given).\n            case \"get\" -> (JavaInvokable) args -> {\n                if (args.length == 0 || args[0] == null) {\n                    throw new RuntimeException(\"context.get() requires a name argument\");\n                }\n                String name = args[0].toString();\n                Object defaultValue = args.length > 1 ? args[1] : null;\n                MarkupScope scope = getMarkupScope();\n                if (scope != null) {\n                    Object v = scope.lookup(name);\n                    if (v != null) return v;\n                }","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/markup/MarkupContext.java#L127-L163","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Pass an actual JSON string: `context.fromJson('{\"a\":1}')`.","Guard optional input: only call fromJson when the string is non-null, or default it to '{}' / 'null' as appropriate.","Check where the JSON string originates (model attribute, read() of a file) and ensure it is loaded before parsing.","If input may be empty-string or malformed, wrap in try-catch — this specific error only covers missing/null arguments."],"exampleFix":"// before\nvar data = context.fromJson(jsonBlob);\n\n// after\nvar data = jsonBlob != null ? context.fromJson(jsonBlob) : {};","handlingStrategy":"validation","validationCode":"function safeFromJson(ctx, s) {\n  if (s == null || s === '') return null;\n  return ctx.fromJson(s);\n}","typeGuard":"function isJsonString(s) { return typeof s === 'string' && s.trim().length > 0; }\n// usage: if (isJsonString(s)) context.fromJson(s);","tryCatchPattern":"try {\n  var obj = context.fromJson(s);\n} catch (e) {\n  if (e.message === 'fromJson() requires a JSON string argument') {\n    obj = null;\n  } else { throw e; }\n}","preventionTips":["Only call fromJson with a non-null JSON string","Default optional JSON blobs to '{}' or 'null' before parsing","Validate upstream JSON strings in tests, not only at render time","Handle parse failures separately — this error only covers missing/null args"],"tags":["markup","javascript-binding","json","missing-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}