Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON.stringify() wraps the output within ` (Grave Accent mark) #294

Open
ASHIQUEMD opened this issue Jul 29, 2024 · 1 comment
Open

JSON.stringify() wraps the output within ` (Grave Accent mark) #294

ASHIQUEMD opened this issue Jul 29, 2024 · 1 comment

Comments

@ASHIQUEMD
Copy link

Issue: if the object contains string data with single quoted value in it, e.g. "Test 'data'", JSON.stringify(results) returns result wrapped in ` (Grave Accent mark).

Actual result:

image

Expected result (without ` ): {"analyzer":"Test 'data'","details":{"range":{"start":"1","end":"2"},"description":"Hello"}}

Sample code:

function main() {
    const results = {
        analyzer: "Test 'data'",
        details: {
            range: {
                start: "1",
                end: "2"
            },
            description: "Hello"
        }
      };

    return JSON.stringify(results);
  }
  
 main();
@n-riesco
Copy link
Owner

You may want to request this feature on https://github.com/nodejs/node/issues instead. As the representation of strings in iJavaScript is based on the Node.js REPL.

It's defined by util.inspect. There are a few options to customise the output of util.inspect, but I don't see any options to achieve what you want (Node v22).

Unlike the REPL, iJavaScript lets you overwrite util.inspect. So, the following workaround may work for you:

const prevInspect = util.inspect;

util.inspect = function inspect(value, opts) {
    let r = prevInspect(value, opts);
    return (typeof value === "string" && r[0] === '`' && r[r.length - 1] === '`') ? r.slice(1, -1) : r;
}

main(); // output: {"analyzer":"Test 'data'","details":{"range":{"start":"1","end":"2"},"description":"Hello"}}

You may also be interested in the API iJavaScript offers to customise outputs. See here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants