JSON.stringify key without quotes
It sounds like you are looking for a data-serialization format that is human-readable and version-control-friendly but not as strict about quotes as JSON.
Such formats include:
- Relaxed JSON (RJSON) (simple keys and simple values generally do not require quotes)
- Hjson (simple keys and simple values generally do not require quotes)
- YAML (keys and values generally do not require quotes)
- JavaScript object literal (also printed out by many implementations of "console.dir()" when passed a JavaScript object; simple keys generally not required to be quoted, but string values must be quoted by either single quotes or double quotes)
for completeness:
JSON (requires double-quotes around keys, also called property names, and requires double-quotes around string data values).
JSON.stringify object with keys without the quotes in subobject
Actually what I needed was easily achievable. All I had to do is to add replace to my return statement in the function
return JSON.stringify(file_data).replace(/"(\w+)"\s*:/g, '$1:');
JSON property value without quote
In JSON, double quotes "
around values indicate strings. Non-strings are not delimited with "
s. Numbers have no delimiters, and the total
property getter, when invoked (by JSON.stringify
) returns a number, so the resulting JSON does not surround the total
value with quotes.
how to create a javascript object without double quotes on the property value
Well I think don't use stringify
that solve your problem try it as it is.
JSON Object With or Without Quotes
You are creating a Javascript Object
. If you want a JSON-string from it, use JSON.stringify
.
So
const myObj = {mykey: "my value"};
const myObjJSON = JSON.stringify(myObj);
Based on comments:
There is no such thing as a JSON Object. There are JSON-strings, which can be parsed
to Javascript Objects. Javascript Objects can be stringified
to JSON strings. Within a JSON-string keys and values are quoted. So the result of the above is a string containing '{"mykey":"my value"}'
.
Try parsing myObjJSON
in your browser console (using: JSON.parse(myObjJSON)
) and you get: Object {mykey: "my value"}
.
Convert Object to string in javascript without the quotes in key
This code is taken from this answer.
There is a regex solution that is simpler but it has a shortcoming that is inherent to regex. For some edge cases in complex and nested objects it does not work.
const data = {a: "b", "b": "c", c: {a: "b", "c": "d"}}
console.log(stringify(data))
function stringify(obj_from_json){ if(typeof obj_from_json !== "object" || Array.isArray(obj_from_json)){ // not an object, stringify using native function return JSON.stringify(obj_from_json); } // Implements recursive object serialization according to JSON spec // but without quotes around the keys. let props = Object .keys(obj_from_json) .map(key => `${key}:${stringify(obj_from_json[key])}`) .join(","); return `{${props}}`;}
json stringify failing when value is without double quotes
The JSON spec outlines what you're seeing as standard behavior and I'd recommend that you operate on the number as a number instead of a string.
If you need to modify this for some reason, you can use a "replacer" function as shown in the JSON.stringify spec.
var numbersAsStringsJSON = JSON.stringify(myData, replacer);
function replacer(key, value) {
if (typeof value === "number") { return String(value); }
else { return value; }
}
See JSFiddle for working example.
Related Topics
Extract Part of Url With Regex
How to Compare Two Arrays Using Lodash (The Order Matters)
What Does an Exclamation Mark Before a Variable Mean in JavaScript
Click on Element Containing Text in Puppeteer
How to Get the Latest and Oldest Record in Mongoose.Js (Or Just the Timespan Between Them)
Can We Have Code After Location.Reload(True)
Adding Script Tag to React/Jsx
Sequlize Order by Only from Main Query
Change the Arrow Buttons in Slick Slider
Get Start Date and End Date of Current Week (Week Start from Monday and End With Sunday )
How to Add Spaces to for Loop Output in JavaScript
How to Change the State Correctly (Read-Only Error)
How to Parse Through Local Json File in React Js
Postman Test - Validating String Values in an Object in an Array
How to Open a Web Page Automatically in Full Screen Mode
Trying to Load a Script File from My Node Server - Get 404