{ "data": { "question": { "questionId": "2745", "questionFrontendId": "2633", "boundTopicId": null, "title": "Convert Object to JSON String", "titleSlug": "convert-object-to-json-string", "content": "

Given an object, return a valid JSON string of that object. You may assume the object only inludes strings, integers, arrays, objects, booleans, and null. The returned string should not include extra spaces. The order of keys should be the same as the order returned by Object.keys().

\n\n

Please solve it without using the built-in JSON.stringify method.

\n\n

 

\n

Example 1:

\n\n
\nInput: object = {"y":1,"x":2}\nOutput: {"y":1,"x":2}\nExplanation: \nReturn the JSON representation.\nNote that the order of keys should be the same as the order returned by Object.keys().
\n\n

Example 2:

\n\n
\nInput: object = {"a":"str","b":-12,"c":true,"d":null}\nOutput: {"a":"str","b":-12,"c":true,"d":null}\nExplanation:\nThe primitives of JSON are strings, numbers, booleans, and null.\n
\n\n

Example 3:

\n\n
\nInput: object = {"key":{"a":1,"b":[{},null,"Hello"]}}\nOutput: {"key":{"a":1,"b":[{},null,"Hello"]}}\nExplanation:\nObjects and arrays can include other objects and arrays.\n
\n\n

Example 4:

\n\n
\nInput: object = true\nOutput: true\nExplanation:\nPrimitive types are valid inputs.
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 15, "dislikes": 3, "isLiked": null, "similarQuestions": "[{\"title\": \"JSON Deep Equal\", \"titleSlug\": \"json-deep-equal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Flatten Deeply Nested Array\", \"titleSlug\": \"flatten-deeply-nested-array\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "exampleTestcases": "{\"y\":1,\"x\":2}\n{\"a\":\"str\",\"b\":-12,\"c\":true,\"d\":null}\n{\"key\":{\"a\":1,\"b\":[{},null,\"Hello\"]}}\ntrue", "categoryTitle": "JavaScript", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {any} object\n * @return {string}\n */\nvar jsonStringify = function(object) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "function jsonStringify(object: any): string {\n\n};", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"347\", \"totalSubmission\": \"442\", \"totalAcceptedRaw\": 347, \"totalSubmissionRaw\": 442, \"acRate\": \"78.5%\"}", "hints": [ "Consider the 4 possibilities. The object could be an array, an object, a string, or another type.", "Think about the problem recursively. If you know how to convert any sub-data into a string, how could you use it to convert the entire data into a string?", "If the data is a string, it's just the value surrounded by double quotes. If the data is another type, its just String(data). If the data is an array, it's the recursively stringified value of each item separated by commas. If the data is an object, it's a series of key-value pairs where each value is the recursively stringified value." ], "solution": null, "status": null, "sampleTestCase": "{\"y\":1,\"x\":2}", "metaData": "{\n \"name\": \"jsonStringify\",\n \"params\": [\n {\n \"name\": \"object\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"typescript\": [\"Typescript\", \"

TypeScript 4.5.4, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2020 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }