Parsing string as JSON with single quotes on key and keyvalue
TL;DR JSON only supports double quotes.
Please fix the error on the server, if this is possible.
JSON required double quotes, the single quotes are therefore not conform to the standard. There may be parsers, that support them anyway, but you can never rely on that. For more details on the JSON syntax, you may have a look at http://www.json.org/.
Besides this, the input string "{'a':'Your's'}";
is totally invalid itself, if the single quotes would be valid. The '
in Your's
is breaking the string literal, the following s
is outside the string and the next '
is opening a string, that contains }
but is never closed by another '
.
The correct syntax would be '{"a":"Your\'s"}'
.
If you received the string and cannot correct the server output, you may try to replace all '
by "
, but you will have problems with single quotes inside your payload strings. By far the easiest - and most stable(!) - fix should be correcting the server output instead of correcting the defect output on the client.
Converting on the client with the following code may be a good idea at the first thought, but would corrupt payload strings with single quotes in it.
replaceInString = function(fullString, search, replacement) {
return fullString.split(search).join(replacement);
};
var json = replaceInString("{'a':'Your's'}", "'", '"');
If you can be sure, there are no whitespace characters outside the payload and also there are no line breaks, you could use the following function. But also only, if you are sure, the search patterns are not in the payload strings.
var json = "{'a':'Your's'}";
replaceInString = function(fullString, search, replacement) {
return fullString.split(search).join(replacement);
};
json = replaceInString(json, "{'", '{"');
json = replaceInString(json, "'}", '"}');
json = replaceInString(json, "':", '":');
json = replaceInString(json, ":'", ':"');
json = replaceInString(json, "',", '",');
json = replaceInString(json, ",'", ',"');
json = replaceInString(json, "['", '["');
json = replaceInString(json, "']", '"]');
But using this code, e.g. the JSON `following JSON string would be corrupted.
{'mathTerm':'x=1-[2+A']'}
To be clear: Code like this gets you over a bump on the road to develop , test or learn something. But this is not a durable fix. Contact the server developer to fix his implementation and remove your client side fix before going to production.
How to make JSON parse a JSON string containing single quotation marks
Instead of trying to treat the data as a string in the JS and parsing it again, you can just inject the value direct into the JS as an array literal. Since JSON is a subset of JS object literal syntax, it'll just be treated as a literal. Don't think you even need the Html.Raw()
command.
Try simply
var SheetsArr = @Model.Sheets;
(I'm assuming here that in the C# you assigned the C# SheetsArr
variable to the Sheets
property of your model before you returned it.)
Then this removes the issue where your single-quoted string in JS is mucked up by single quotes within the JSON string.
To illustrate the point, let's say that Model.Sheets
contains a JSON string like this:
[{ "SomeProp": "Hello, 'Friend'"}, { "SomeProp": "Hello again"}]
You can see the single quote marks.
In your original code, executing the Razor code with that string in it would result in the following JavaScript:
var SheetsArr = JSON.parse('[{ "SomeProp": "Hello, 'Friend'"}, { "SomeProp": "Hello again"}]');
JSON parse - single quote inside name
I can think of two possibilities:
Create a script element of type application/json
, inject your template data into it, then read its data, eg.
<script id="place-json" type="application/json">
{{place.json|safe}}
</script>
<script type="application/javascript">
P.place = $('#place-json').text();
</script>
Or, manually escape the single quotes before injecting the string, eg.
simplejson.dumps(yourdata).replace("'", r"\'")
Parsing a single quotes json string clickhouse
That's not a single quote JSON, it's only a print format of JSON Dict.
But you can do this to convert it into a JSON:
SELECT JSONExtractString(
replaceAll(
replaceAll(
'{''name'': None, ''medium'': ''organic'', ''source'': ''google-play''}', '''', '"'
), 'None', 'null'
), 'medium'
)
How to convert this String to JSON object in Javascript, when the string has single quote within the double quote
If you know that the only issue is that the property names are single quoted rather than the JSON required double quotes you could use regex to replace the single quotes on property names with doubles.
var s=`{'error': "No such file or directory: '../FileSystem/3434-5433-124/'"} `
const regex = /(')(\S*)('):/g
s = s.replace(regex, '"$2":')
const workingJson = JSON.parse(s);
Should do the trick. This will replace single quotes with doubles for any part of your string that has the format of (single quote)(text)(single quote)(colon), this will most likely only be property names but keep in mind that if another part of your string follows this exact format it will also get it's single quotes replaced by doubles.
Related Topics
Reactjs - Multiple If Conditions Inside Map Function
Keep Order of Objects Inside a Json String After They Are Parsed
How to Change Font-Size in Vuetify Component
Check Checkbox and Trigger Change Event JavaScript
How to Download a File With Node.Js (Without Using Third-Party Libraries)
Jquery - Get Id Dynamically from Generated Element
How to Add a Simple Onclick Event Handler to a Canvas Element
How to Validate Mobile Number for Countries in Angular
Split String in JavaScript and Detect Line Break
How to Change Image and Text Color When Clicking Using React-Native
Write a Auto-Fill and Submit Web Form Program
Angular: Toggle Active Class on Only Button the Current Clicked Button (Not Using *Ngfor)
Javascript and Getelementbyid for Multiple Elements With the Same Id
Filter Input Text to Enter Only Number and Not Even Decimal
Laravel - How to Pass a PHP Variable to a JavaScript Function from a Blade File
How to Make an Empty String Array in Typescript
React Js - How to Get Click Event Values from Child Component to Parent Component