{"record":{"id":"a31b31ddffbc00a6","repo":"karatelabs/karate","slug":"tojson-needs-at-least-one-argument","errorCode":null,"errorMessage":"toJson() needs at least one argument","messagePattern":"toJson\\(\\) needs at least one argument","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":585,"sourceCode":"            }\n            List<Object> list = (List<Object>) arg;\n            byte[] bytes = new byte[list.size()];\n            for (int i = 0; i < list.size(); i++) {\n                Object item = list.get(i);\n                if (item instanceof Number num) {\n                    bytes[i] = num.byteValue();\n                } else {\n                    throw new RuntimeException(\"toBytes() list must contain only numbers, got: \" + item.getClass().getName() + \" at index \" + i);\n                }\n            }\n            return bytes;\n        };\n    }\n\n    static JavaInvokable toJson() {\n        return args -> {\n            if (args.length < 1) {\n                throw new RuntimeException(\"toJson() needs at least one argument\");\n            }\n            Object obj = args[0];\n            boolean removeNulls = args.length > 1 && Boolean.TRUE.equals(args[1]);\n            Object result = Json.of(obj).value();\n            if (removeNulls) {\n                removeNullValues(result);\n            }\n            return result;\n        };\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    private static void removeNullValues(Object obj) {\n        if (obj instanceof Map) {\n            Map<String, Object> map = (Map<String, Object>) obj;\n            map.entrySet().removeIf(e -> e.getValue() == null);\n            map.values().forEach(KarateJsUtils::removeNullValues);\n        } else if (obj instanceof List) {","sourceCodeStart":567,"sourceCodeEnd":603,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L567-L603","documentation":"karate.toJson() converts an object to its JSON representation via Json.of(obj), optionally removing null values when the second argument is true. The library throws this error when called with no argument, since there is nothing to serialize. Additional arguments beyond the second are ignored.","triggerScenarios":"karate.toJson() with zero arguments; calling the function reference without arguments; the object variable being dropped during refactoring.","commonSituations":"Serializing JS objects or Maps for logging or comparison; using toJson(obj, true) to strip nulls before embedding JSON in requests; dynamic call construction losing the argument.","solutions":["Pass the object: karate.toJson(myObj).","To remove null values, pass true as the second argument: karate.toJson(obj, true).","Confirm the variable to serialize exists and is in scope."],"exampleFix":"// before\nvar json = karate.toJson()\n// after\nvar json = karate.toJson(payload)","handlingStrategy":"type-guard","validationCode":"if (obj == null) { throw new Error('toJson() requires an object to serialize') }","typeGuard":"function canToJson(v) { return v !== undefined }","tryCatchPattern":"try { var json = karate.toJson(obj) } catch (e) { if (('' + e).indexOf('needs at least one argument') >= 0) { throw new Error('toJson(): missing object argument') } throw e }","preventionTips":["Pass the object explicitly as the first argument","Pass true as second argument to strip nulls rather than post-processing","Check the variable exists and is in scope before serializing"],"tags":["javascript","argument-validation","json"],"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"}