#3043·karate

2.1.2: JSON.stringify() drops quotes when serializing a scalar string

Author: sentrybottaleCreated Sep 15, 2026Updated Sep 17, 2026
Labelsbugfixed

Description

In Karate 2.1.2, JSON.stringify('alpha') returns alpha instead of the JSON string literal "alpha". The comparison fails even inside a single JavaScript function, so this is reproducible without browser interaction or passing the serialized result through a feature variable.

The unchanged reproducer passes on Karate 1.5.2. This breaks code that safely embeds strings into JavaScript expressions: for example, concatenating a serialized CSS selector produces document.querySelector(#sample) instead of document.querySelector("#sample").

Steps to Reproduce

  1. Create an empty directory and download the Karate 2.1.2 standalone JAR.
  2. Save this as repro.feature:
gherkin
Feature: JSON stringify scalar string

Scenario: A scalar string must serialize as a quoted JSON string
  * def isCorrect = function(){ return JSON.stringify('alpha') === '"alpha"' }
  * match isCorrect() == true
  1. Run:
bash
java -jar karate-2.1.2.jar run -o report-2.1.2 repro.feature
  1. For comparison, download the Karate 1.5.2 standalone JAR and run the identical file:
bash
java -jar karate-1.5.2.jar -o report-1.5.2 repro.feature

No browser, server, credentials, configuration file, called features, or extra dependencies are required.

Expected Behavior

JSON.stringify('alpha') should return a string containing the enclosing double quotes: "alpha". The equality check should return true, and the scenario should pass.

Actual Behavior

On 2.1.2, the equality check returns false and the scenario fails:

* match isCorrect() == true
match failed: EQUALS
  $ | not equal (BOOLEAN:BOOLEAN)
  false
  true

Verified using the same Java installation:

Karate Result CLI exit code
2.1.2 1 scenario failed 1
1.5.2 1 scenario passed 0

Additional isolated checks on 2.1.2 show that JSON.stringify(['alpha']) correctly returns ["alpha"], and JSON.stringify({name:'alpha'}) correctly returns {"name":"alpha"}. The failure above concerns a string at the root.

Karate Version

2.1.2 standalone JAR; comparison: 1.5.2.

Java Version

Oracle Java 24.0.2 (build 24.0.2+12-54).

Operating System

macOS