If I have the object literal:
{a: "hello"}
Is there a Javascript function to convert this object into a literal string, so that the output would be the literal syntax:
'{a: "hello"}'
With JSON.stringify
the output would be
'{"a": "hello"}'
If I have the object literal:
{a: "hello"}
Is there a Javascript function to convert this object into a literal string, so that the output would be the literal syntax:
'{a: "hello"}'
With JSON.stringify
the output would be
'{"a": "hello"}'
Share
Improve this question
edited Jan 29, 2019 at 20:31
Bharata
14.2k6 gold badges43 silver badges53 bronze badges
asked Jan 29, 2019 at 19:34
sir-haversir-haver
3,59210 gold badges56 silver badges109 bronze badges
7
- 1 One problem is that once the object exists, it's not possible to know how identifier-like property names were expressed when the object was created (or subsequent properties added). They may or may not have had quotes. – Pointy Commented Jan 29, 2019 at 19:38
- 1 Unsure what your objective is, but you could (mis)use JSON.stringify()... – Stuart Commented Jan 29, 2019 at 19:39
- Maybe this can help stackoverflow./a/5612876/3565132 – Haris Bouchlis Commented Jan 29, 2019 at 19:40
-
2
Just to be clear, though,
{"a":"hello"}
is a legal literal object representation, so I'm not sure why the JSON representation isn't sufficient for your purpose. – Paul Commented Jan 29, 2019 at 19:41 - 4 Fixing the server so it supports JSON instead of a non-standard data format would be a better bet. – Quentin Commented Jan 29, 2019 at 19:58
4 Answers
Reset to default 7You can do it with JSON.stringify
and then with String.replace
like follows:
var jsObj =
{
abc: "hello",
bca: "allo",
cab: "dd:cc",
d: ["hello", "llo", "dd:cc"],
e: {abc: "hello", bca: "allo", cab: "dd:cc"}
};
function format(obj)
{
var str = JSON.stringify(obj, 0, 4),
arr = str.match(/".*?":/g);
for(var i = 0; i < arr.length; i++)
str = str.replace(arr[i], arr[i].replace(/"/g,''));
return str;
}
console.log(format(jsObj));
JavaScript has no built-in functions that will convert an object to a string representation of it which either:
- Uses identifiers instead of strings for property names
- Represents the original syntax used to create the object
You could write your own function for the former (at least when the property name can be represented as a literal) but the latter is impossible as JavaScript stores no information about the source code used to create the object in the first place.
Ok just for fun...roll your own?
const stringify = (obj) => {
// Iterate over keys, reducing to a string
let str = Object.keys(obj).reduce((acc, cur) => {
let next = `${cur}: "${obj[cur]}"`;
return acc
? `${acc}, ${next}`
: `{${next}`;
}, '');
// Return, appending final '}'
return `${str}}`;
}
document.write(stringify({
foo:1,
bar:'seat'
}));
That said, your exact requirements aren't clear so I'm not sure this will meet them. But it might be a starting point if there's no native solution that works.
It does convert it to the literal syntax. You are able to create objects with multiple forms of syntax. Both of the following object declarations are valid:
var a = {a: "a"}
var b = {"b": "b"}
If you want to remove the "" around the key you should be able to match them with the following regex /\"(.*?)\":/g and replace them with something like this:
function reformat(str) {
var myRegexp = /\"(.*?)\":/g;
match = myRegexp.exec(str);
while (match != null) {
str = str.replace(match[0], match[1] + ":");
match = myRegexp.exec(str);
}
return str;
}
Hope that helps :)