{"record":{"id":"3b8890962c8b5620","repo":"karatelabs/karate","slug":"fromjson-needs-one-argument-a-json-string","errorCode":null,"errorMessage":"fromJson() needs one argument: a JSON string","messagePattern":"fromJson\\(\\) needs one argument: a JSON string","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":263,"sourceCode":"            if (collection instanceof List<?> list) {\n                for (int i = 0; i < list.size(); i++) {\n                    fn.call(null, new Object[]{list.get(i), i});\n                }\n            } else if (collection instanceof Map) {\n                Map<String, Object> map = (Map<String, Object>) collection;\n                int i = 0;\n                for (Map.Entry<String, Object> entry : map.entrySet()) {\n                    fn.call(null, new Object[]{entry.getKey(), entry.getValue(), i++});\n                }\n            }\n            return null;\n        };\n    }\n\n    static JavaInvokable fromJson() {\n        return args -> {\n            if (args.length < 1) {\n                throw new RuntimeException(\"fromJson() needs one argument: a JSON string\");\n            }\n            Object arg = args[0];\n            if (arg instanceof String str) {\n                return Json.of(str).value();\n            } else {\n                // Already parsed, just return it\n                return arg;\n            }\n        };\n    }\n\n    static JavaInvokable jsonPath() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"jsonPath() needs two arguments: object and path\");\n            }\n            Object json = args[0];\n            if (json instanceof Node node) {","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L245-L281","documentation":"Karate's fromJson() JS utility parses a JSON string into a Java object graph. It requires one string argument; calling it with no arguments throws this error. Non-string arguments are tolerated (returned as already-parsed values), so only the missing-argument case triggers this.","triggerScenarios":"Calling fromJson() with zero arguments, e.g. fromJson(), or passing undefined where a JSON string was expected.","commonSituations":"Variable holding the JSON string is undefined/null due to a failed earlier step; forgetting the argument when converting from Json.parse style code.","solutions":["Pass the JSON string: fromJson('{\"a\":1}')","Check the variable feeding fromJson is actually defined and a string","If the value is already an object, fromJson can be dropped entirely"],"exampleFix":"// before\nvar obj = karate.fromJson();\n// after\nvar obj = karate.fromJson('{\"a\":1}');","handlingStrategy":"type-guard","validationCode":"if (typeof json !== 'string') throw new Error('fromJson expects a JSON string');","typeGuard":"function canParseJson(v) { return typeof v === 'string' && v.trim().length > 0; }","tryCatchPattern":"try { obj = karate.fromJson(str); } catch (e) { karate.log('fromJson failed: ' + e.message); }","preventionTips":["Check the source variable is a defined string before parsing","Skip fromJson if the value is already an object (it passes through)","Log the payload when a previous step might have left it undefined"],"tags":["argument-count","json","javascript","karate"],"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"}