I am trying to stringify the object which has another stringify object. I am getting \added to inside object
a = {}
str = {'a': 'test'}
a.str = JSON.stringify(str);
console.log("=="+ (a));
console.log("strin " + JSON.stringify(a) ) // {"str":"{\"a\":\"test\"}"}
expected: {"str":"{"a":"test"}"}
What you expect would not be valid JSON.
Quotes are used to delimit strings in a JSON text.
With your expected result a JSON parser would see "{" and think that was the whole string and then a would be an error.
The escape sequence \" is how you say "This is a quote that is a part of a string" instead of "This is a quote that ends the string".
The output is fine. There is nothing wrong.
That said, nesting JSON is generally a bad idea. It is more complicated to parse and harder to read.
In general you should be creating the complete data structure and then stringifying that.
const a = {};
const str = {
'a': 'test'
};
a.str = str;
const json = JSON.stringify(a, null, 2);
console.log(`result: ${json}`);
There is an error
str = {'a': 'test}
It should be
str = {'a': 'test'}
The reason you are getting '' is simply because they are escaping double strings
This is illegal:
"{"str":"{"a":"test"}"}"
This is legal:
"{\"str\":\"{\"a\":\"test\"}\"}"
Related
I have a huge JSON stringified into a string like this one:
"\\u007B\\u0022name\\u0022\\u003A\\u0022T\\u0065st\\u0022}"
I need to JSON.parse it to use as an object. Do you know any way to decode it?
I tried decodeURIComponent(), unescape(), different variants of .replace( /\\u/g, "\u" ) and I can not get it into the needed form.
You can convert UTF-16 to text using the following function:
function utf16ToText(s) {
return s.replace(/\\u[0-9a-fA-F]{4}/gi, match => {
return String.fromCharCode(parseInt(match.replace(/\\u/g, ""), 16));
});
}
Demo:
const r = utf16ToText("\\u007B\\u0022name\\u0022\\u003A\\u0022T\\u0065st\\u0022\\u007d");
console.log("As text: ", r);
const j = JSON.parse(r);
console.log("As JSON: ", j);
console.log("JSON Prop: ", j.name);
function utf16ToText(s) {
return s.replace(/\\u[0-9a-fA-F]{4}/gi, match => {
return String.fromCharCode(parseInt(match.replace(/\\u/g, ""), 16));
});
}
If your string is valid JSON, then you can use JSON.parse() to process the UTF16 for you. To make what you have valid JSON, you have to add double quotes to each end (inside the actual string) as all strings must be enclosed in double quotes in the JSON format. Here's an example:
let data = "\\u007B\\u0022name\\u0022\\u003A\\u0022T\\u0065st\\u0022\\u007d";
// to make this legal JSON so we can let the JSON parser parse it for us and
// handle the UTF16 for us, we need to put double quotes in the actual string at each end
// Then, it's legal JSON and we can parse it
let str = JSON.parse('"' + data + '"');
console.log(str);
console.log("type is", typeof str);
This gives you a result in string form:
{"name":"Test"}
This result is now legal JSON on its own. If you then wanted to parse that as JSON, could just call JSON.parse() on it again to turn it into an actual Javascript object:
let data = "\\u007B\\u0022name\\u0022\\u003A\\u0022T\\u0065st\\u0022\\u007d";
let str = JSON.parse('"' + data + '"'); // decoded string here
// now take the string and actually parse it into a Javascript object
let obj = JSON.parse(str);
console.log(obj); // Javascript object here
console.log("type is", typeof obj);
This gives you a resulting live Javascript object:
{name:"Test"}
The first call to JSON.parse() just takes your JSON string and decodes it into a Javascript string. Since that Javascript string is now also legal JSON for an object definition, you can call JSON.parse() on it again to turn it into a Javascript object.
Thank you for your input. Because of that, I got the solution:
let a = "\\u007B\\u0022name\\u0022\\u003A\\u0022T\\u0065st\\u0022}" original string
let b = '"' + a + '"' adding " in order to make a valid stringified JSON
let c = JSON.parse(b) that will produce a partially decoded string (partially because some of the \uXXXX characters can stay in the string)
let solution = JSON.parse(c) that will create an object with all the characters decoded
Big thanks to #jfriend00
I want to remove some special character from json without parsing
the json into object.
Parsing would result into error that is why i wanted to do without json.parse().
below is my json:
{
"id":324,
"name":"first",
"body":{
"sbody": "<p>\\\The New Stroy\\\</p>"
}
}
desired output:
{
"id":324,
"name":"first",
"body":{
"sbody": "<p> The New Stroy </p>"
}
}
Looks like your input is a string and the error you are getting is when using JSON.parse.
Try this
var response = '{"sbody": "<p>\\\The New Stroy\\\</p>"}';
response = response.replace(/\\/g, "");
var obj = JSON.parse(response);
console.log(obj);
You need to run .replace on your string:
var string = '{"id":324,"name":"first","body":{"sbody":"<p>\\\The New Stroy\\\</p>"}}';
string = string.replace(/\\/g,'');
console.log(string);
//{"id":324,"name":"first","body":{"sbody":"<p>The New Stroy</p>"}}
The reason the pattern is /\\/ is because \ is used to escape characters. With a single \ we end up escaping the /. What we need to do here is escape the escape character to turn it into a literal string character: \\.
The g after the pattern means to search for the pattern "globally" in the string, so we replace all instances of it.
var obj = {
"id":324,
"name":"first",
"body":{
"sbody": "<p>\\\The New Stroy\\\</p>"
}
}
// Convert object to string
var str = JSON.stringify(obj);
// Remove \ from the string
var convertedStr= str.replace(/\\/g,'');
// Convert updated string back to object
var newObj = JSON.parse(convertedStr);
I am creating a cli app & I get an input from the user : print [{name:'joe', age:19}] where arg = [{name:'joe', age:19}]
But when I do typeof arg, it returns string. I tried using json.parse [throws error], slice(1,-1) [removes outer array brackets & type remains string] and Array.from(arg) [splits all the brackets & letters into different elements].
So how do I convert [{name:'joe', age:19}] into type object array?
Code Snippet :
vorpal
.command('input <array>')
.action(function(args,cb) {
let array = args.array;
this.log(array); //returns [{name:'joe',age:19}]
this.log(typeof array); //returns string
cb();
});
JSON.parse expects keys to be in double quotes. so somehow you have to change the input to this format
JSON.parse([{"name":"joe", "age":"19"}])
and the output would be
The input that you pass is not a valid JSON structure that's why JSON.parse has shown parsing error.
var validInput = '[{"name":"joe","age":19}]';
console.log(JSON.parse(validInput));
Run Code Snippet.
var obj = new Object({name:'joe', age:19});
console.log(typeof obj);
You can still try these regexes, that will convert your string into valid JSON. Be careful that it's not perfect, this version only accepts letters and numbers as keys for objects, while other characters can be valid too for object keys. You have to modify this part [a-zA-Z0-9] to improve it if you know a bit of regex..
var str = "[{name:'joe', age:19}, {'name':'jack', age:21}, {\"name\":\"jane\", age:23}]", validJson, obj;
console.log('original string: ', str);
validJson = str.replace(/({\s*|,\s*)'?([a-zA-Z0-9]+)'?(\s*:)/g, '$1"$2"$3').replace(/(:\s*)'(.*?)'(\s*,|\s*})/g, '$1"$2"$3');
console.log('modified string: ', validJson);
obj = JSON.parse(validJson);
console.log('decoded from JSON: ', obj);
EDIT: regex updated to support object keys defined with simple quotes (2nd object). NOTE: it keeps already well formatted JSON as it is (3rd object).
One solution is to use eval, e.g.
> eval("[{name:'joe', age:19}]")
[ { name: 'joe', age: 19 } ]
My node app receives a series of strings in the format "a=x b=y c=z" (i.e. a string containing several space-separated key=value pairs).
What is the neatest way of converting such a string into a JSON object of the form {a: x, b: y, c: z}?
I'm betting that there's a one-line solution, but haven't managed to find it yet.
Thanks.
One way would be to replace the with a , and an = with a ::
var jsonStr = '{' + str.replace(/ /g, ', ').replace(/=/g, ': ') + '}';
Or if you need quotes around the keys and values:
var jsonStr2 = '{"' + str.replace(/ /g, '", "').replace(/=/g, '": "') + '"}';
JSON.parse() it if you need.
Sample output:
str: a=x b=y c=z
jsonStr: {a: x, b: y, c: z}
jsonStr2: {"a": "x", "b": "y", "c": "z"}
Building on John Bupit's excellent answer, I have made a couple of further enhancements to end up with the following (the string being parsed being in message):
var json = JSON.parse(('{"' + message.replace(/^\s+|\s+$/g,'').replace(/=(?=\s|$)/g, '="" ').replace(/\s+(?=([^"]*"[^"]*")*[^"]*$)/g, '", "').replace(/=/g, '": "') + '"}').replace(/""/g, '"'));
Basically the scheme is as follows:
First replace(): trim off any leading or trailing whitespace -- equivalent to trim()
Second replace(): add double quotes (empty string) for any value that is completely missing (e.g. key1= key2=val goes to key1="" key2=val).
Third replace(): replace each space (which acts as a delimiter) with ", ", but not where the space is within double quotes (i.e. part of a string value).
Fourth replace(): replace each = with ": "
Wrap the entire string up as follows: {"..."}
Finally, replace any double quotes "" created by the above steps (because the value string was already wrapped in quotes in message) with single quotes "
Even more finally, run JSON.parse() over the result.
The above scheme should cope with missing values, with some values being quoted and some unquoted, and with spaces within value strings, e.g. something like a= b="x" c="y y" d=z.
Assuming that you don't get nested objects in that format :
var sample = 'a=x b=y c=z';
var newobj = {};
sample.split(' ').forEach(function (value) {
var keypair = value.split('=');
newobj[keypair[0]] = keypair[1];
});
console.dir(newobj);
What this does is split on every white-space and push to an array, and the array is looped and each item in array is split again to get each key-value pair which is assigned to the newobj.
Here's a simple function that will do the trick
function stringToObj (string) {
var obj = {};
var stringArray = string.split(' ');
for(var i = 0; i < stringArray.length; i++){
var kvp = stringArray[i].split('=');
if(kvp[1]){
obj[kvp[0]] = kvp[1]
}
}
return obj;
}
newstr = ""
for kvp in #value.split(" ")
newstr += kvp.replace(/=/,'":"').replace(/^/, '"').replace(/$/, '"').replace(/\"\"/,'" "')
newstr = newstr.replace(/\"\"/g, '","')
jsn = JSON.parse('{' + newstr + '}')
I created a simple online tool for similar need: https://superal.github.io/online-tools/
Use cases:
To transfer key:value pairs copied from chrome network requests(form data or query string parameters) or postman headers key-value(in bulk edit style) to json format.
For example:
key:value pairs
platform:2
limit:10
start_time:1521561600
end_time:1522080000
offset:0
to json format
{
"platform": "2",
"limit": "10",
"start_time": "1521561600",
"end_time": "1522080000",
"offset": "0"
}
It can be parsed (converted to json) using the help of this npm athena-struct-parser package.
For more information about the package -- https://www.npmjs.com/package/athena-struct-parser
Sample Nodejs Code
var parseStruct =require('athena-struct-parser') ;
var str = '{description=Check the Primary key count of TXN_EVENT table in Oracle, datastore_order=1, zone=yellow, aggregation_type=count, updatedcount=0, updatedat=[2021-06-09T02:03:20.243Z]}'
var parseObj = parseStruct(str)
console.log(parseObj);
Sample string with key=value format taken
{description=Check the Primary key count of TXN_EVENT table in Oracle, datastore_order=1, zone=yellow, aggregation_type=count, updatedcount=0, updatedat=[2021-06-09T02:03:20.243Z]}
Result Parsed output
{
description: 'Check the Primary key count of TXN_EVENT table in Oracle',
datastore_order: '1',
zone: 'yellow',
aggregation_type: 'count',
updatedcount: '0',
updatedat: [ '2021-06-09T02:03:20.004Z' ]
}
I would use an approach leveraging URLSearchParams and Object.fromEntries() like so:
const input = "a=x b=y c=z";
const queryString = input.replaceAll(" ", "&");
const query = new URLSearchParams(queryString);
const output = Object.fromEntries(query);
console.log(output);
Breakdown:
The URLSearchParams constructor takes a string of key-value pairs joined by "&" as it's argument, and parses it into a URLSearchParams object instance. So to use this, the space separators in the original input need to be replaced with a "&" character.
The URLSearchParams instance we have after parsing is an iterable, so we can transform it into a plain Object with Object.fromEntries().
It's not too bad as a one-liner either:
const input = "a=x b=y c=z";
const output = Object.fromEntries(new URLSearchParams(input.replaceAll(" ", "&")));
I have a string that looks like an array: "[918,919]". I would like to convert it to an array, is there an easier way to do this than to split and check if it is a number? Thanks
Use JSON.parse.
var myArray = JSON.parse("[918,919]");
You can get rid of the brackets at the beginning and the end, then use:
str.split(",")
which will return an array split by the comma character.
EDIT
var temp = new Array();
temp = "[918,919]".slice( 1, -1).split(",");
for (a in temp ) {
temp[a] = parseInt(temp[a]);
}
If you use JSON.parse the string must have " and not ' otherwise the code will fail.
for example:
let my_safe_string = "['foo','bar',123]";
let myArray = JSON.parse(my_safe_string)
the code will fail with
Uncaught SyntaxError: Unexpected token ' in JSON at position 1
instead if you use " all will work
let my_safe_string = "["foo","bar",123]";
let myArray = JSON.parse(my_safe_string);
so you have two possibility to cast string array like to array:
Replace ' with " my_safe_string.replace("'",'"'); and after do JSON.parse
If you are extremely sure that your string contain only string array you can use eval:
example:
let myArray = eval(my_safe_string );