How can i remove double quotes from JSON data in react native - javascript

Here is my code
I am not able to remove double quotes from json data and store in Async storage

Related

JSON breaking with quotes

I am getting a Broken JSON, with quotes inside quotes. I can't convert it, because of the error;
{
"PENDENCIA": "Whatever",
"RESULTADO": "Teste "blabla"."
}
What you're looking for is how to "escape" characters in JSON. Because JSON uses quotes around all of the string values ("Whatever"), you can't just include a quote inside your string -- there's no way JSON can figure out that "Teste " isn't the full string but instead should be continued until the final ".
You will need to rewrite the JSON to be "Teste \"blabla\"."
If the JSON is being automatically generated, you'll need to fix whatever is generating your JSON.

An Instance of Needing Double JSON.parse() for Text Response

This isn't so much a problem as a solution I found, and have no idea how it's working. I was making a fetch call and receiving a text response that I needed to convert to workable JSON.
"[{\"Rank\":1,\"FISHERMAN_PHONENAME\":\"James Elam\"...}]"
One JSON.parse knocked off the end double quotes and the slashes, but it still wasn't coming back as the json array of objects I needed so I ended up doing this:
if (response.ok) {
const payload = await response.text();
data = JSON.parse(JSON.parse(payload));
}
That did the trick, but I have no idea why it needed a double parse to make it happen. Any insight would be awesome.
I have no idea why it needed a double parse to make it happen.
Because the data was double encoded as JSON. JSON would simply be
{"Rank":1,"FISHERMAN_PHONENAME":"James Elam"...}
While
"[{\"Rank\":1,\"FISHERMAN_PHONENAME\":\"James Elam\"...}]"
is also valid JSON, in this case it's a (JSON) string that contains other JSON data.
Here is a simply example that demonstrates double encoding:
console.log('Once', JSON.stringify({foo: 42}));
console.log('Twice', JSON.stringify(JSON.stringify({foo: 42})));
Note how the first one does not include leading and trailing quotes.
You should fix the process that generates the JSON to only encode it once.

Trying to remove quotes from arrayed JSON keys

I have this array of javascript objects (which are actually musical notes) that are generated in my javascript front-end. In order to store it, I had to run javascript JSON.stringify(objectArray)before throwing it into a hidden input, but that automatically encases all my keys in double quotes like so:
[
{"class":"barline","symbol":"standard","barline":true,"newSystem":true},
{"class":"note","rhythm":"half","duration":0.5,"symbol":"flag","hand":"R","newbar":true,"rebel":false},
{"class":"note","rhythm":"half","duration":0.5,"symbol":"flag","hand":"R","endbar":true,"rebel":false},
{"class":"barline","symbol":"standard","barline":true},
{"class":"note","rhythm":"half","duration":0.5,"symbol":"flag","hand":"R","newbar":true,"rebel":false}
]
Before filtering my params in Rails, I run JSON.parse(params[:score][:notes]) to turn it from a string into a proper JSON array for storage in MongoDB (I'm specifically using Mongoid)
I know it's normally proper procedure to keep keys in quotes in most cases, but I like using the dot notation for keys to grab values in all my JS.
Should I switch my JS to reference everything with brackets, or can you think of a simple function that would quickly parse out the keys' quotes before sending to the hidden input?
I was looking to do something similar to pass linting, and this regex finds all quotes before semicolons in the string:
"(\w*)":
And replace that with just the text, excluding the quotes:
$1:

How to deserialize Json object which contain control character using Dojo.fromJson

I am using Dojo.fromJson to convert json string to javascript object, but throw exception. Because, there are control characters such as ',\n,\r in the json string.
How can I solve this problem in dojo? convert json string to javascript object, even if there are control characters.
I use Newtonsoft.JsonConvert.SerializeObject to convert C# oject to json data. Json Object: {"name":"'\"abc\n123\r"} then, I use Dojo.fromJson(' {"name":"'\"abc\n123\r"}') to convert json data to javascript object.
Thank you very much!
Problem, i believe is the double-quote which should be escaped by triple backslashes. You can use "native browser JSON decode" as searchterm for "dojo fromJson" synonym.
Without knowing my way around C# - I havent tested but i believe following should work:
string c_sharp_name = "'\"abc\n123\r";
// C#Object.name
c_sharp_name = c_sharp_name.
replace('"', '\\"'). // maybe add a slash on serverside
replace('\n', '\\\n').
replace('\r', '\\\r');
since
while this fails:
{"name":"'\"abc\n123\r"} // your single backslash
this would work:
{"name":"'\\\"abc\\\n123\\\r"} // working triple backslash escape

Django json single and double quotes?

I noticed that if you use the simplejson function in django all the strings are enclosed with single quotes, and the whole json object string is enclosed in double quotes. When I take this string and hand it in to JSON.parse, it gives me an error because they want to use single quotes to enclose the whole object and double quotes for the strings. I could switch them with javascript replace, but then I'd have to take into consideration cases like apostrophes, but I'm sure there is a better way. Is there a way to get django's simplejson to output the object string to the format of JSON.parse?
More info:
django view:
def view(request):
list = [{"a":"apple",},]
return HttpResponse(simplejson.dumps(str(list)), mimetype="application/json")
what the javascript string turn out to be
"[{'a': 'apple'}]"
update
remove the str() around list, simply simplejson.dumps(list). str() trans the list to a string, thus you got "[{'a': 'apple'}]" in client side.
Can you update the question to demo where simplejson encloses strings w/ single quotes?
django.utils.simplejson, normally, conforms w/ JSON specification and does not use single quotes to wrap things. If you mean
>>> from django.utils.simplejson import dumps
>>> dumps("Hello")
'"Hello"' # single quotes here
>>> repr(dumps("Hello"))
'\'"Hello"\'' # or here
They're notations of Python, you don't want to directly use them in JSON.parse (the first one is OK though).

Categories