I am converting a csv file into JSON, and the converter adds " at the beginning of lines, and also adds a \ to quotes.
So, if my CSV file has an array like
"items": [{"name":"item 1"...
the resulting JSON from the converter is:
"items": "[{\"name\":\"item 1\"...
I need the JSON to look like the original CSV entry.
So, how do I change "[{ to [{ ?
Note: I can't just remove quotes everywhere, as they are correct in certain spots. So I need to just remove quotes when they are followed by these specific characters, so only remove "[{
And how do I remove the \ everywhere they appear?
EDIT
Some of the comments and answers have mentioned maybe there is a better way to convert the CSV into a JSON file. In my case, I have data in excel that I need to ultimately be readable by a Lambda function, so I expect I need a way to convert excel to JSON. I have posted a separate question asking for assistance on that topic here .
First of all you should try to generate the correct json format, so that you don't have to go through this issue at all.
However if you have no control over the output of the json data that you're getting, you can simply remove the \" parts by using this regexp:
your_json_var.toString().replace(/\"/g,'');
Where your_json_var contains the returned data.
your_json_var = '"items": "[{\"name\":\"item 1\"...';
var new_var = your_json_var.toString().replace(/\"/g,'');
console.log(new_var);
You could do this
Let stringJson=JSON.stringify(myjson)
Let clearJson = JSON.parse(stringJson.replace(/\"[{/g, '[{').replace(/\/g,'')
Related
I'm reading data using Javascript fetch from an Azure Table Storage, where one of the columns contain a JSON string. So I'm getting back a string that I don't know how to format so that I can parse it.
let jsonData =
'{"Timestamp":"2019-10-01T14:19:48.2593745+00:00","Data":"{\"app_id\":\"apple\",\"dev_id\":\"node1\",\"hardware_serial\":\"001122334455667788\",\"port\":1,\"counter\":63,\"payload_raw\":\"afIWk0ccABwM1Ag=\",\"payload_fields\":{\"doorState\":\"open\",\"humidity\":31,\"location\":{\"lat\":0,\"lon\":0},\"temperature\":22.6},\"metadata\":{\"time\":\"2019-10-01T14:19:48.009374928Z\",\"frequency\":867.9,\"modulation\":\"LORA\",\"data_rate\":\"SF7BW125\",\"coding_rate\":\"4/5\",\"gateways\":[{\"gtw_id\":\"banana\",\"gtw_trusted\":true,\"timestamp\":3579682692,\"time\":\"2019-10-01T14:19:47Z\",\"channel\":7,\"rssi\":-118,\"snr\":-5,\"rf_chain\":0,\"latitude\":0,\"longitude\":0,\"altitude\":172,\"location_source\":\"registry\"},{\"gtw_id\":\"banana\",\"timestamp\":567219172,\"time\":\"2019-10-01T14:19:47.98531Z\",\"channel\":7,\"rssi\":-55,\"snr\":10,\"rf_chain\":0,\"latitude\":0,\"longitude\":0,\"location_source\":\"registry\"}]},\"downlink_url\":\"https://www.someurl.com\"}"}';
After some testing I see that the escape backslashes cause problems, in addition to the quotation marks around the inner JSON object.
In short, parsing this fails:
let jsonData2 = '{"Test":"21342345","Data":"{\"test\":\"ethneipnrt\"}"}';
But this works:
let jsonData2 = '{"Test":"21342345","Data":{"test":"ethneipnrt"}}';
But how can I format the string automatically so that parsing works?
I got the parsing to work yesterday by filtering away the unwanted elements. First replacing all instances of '"{' with '{', then replacing all instances of '}"' with '}', and finally removing all backslashes.
let data = jsonData.replace(/"{/g, '{').replace(/}"/g, '}').replace(/\\/g, '');
let parsed = JSON.parse(data);
I agree that this should probably be handled in the backend. But for now it's enough to get the page up and running.
I am creating an JSON file which stores some Physics equation, which will be rendered using MathJax.
"equations": [
"$w = F.s\cos\theta$"
]
I am getting a bad string error. I have tried adding another backslash before the slashes but that changes the equations drastically.
Is there any way to fix this issue without changing the equation
There were two issues you were falling over.
Firstly, a valid JSON file will have { and } around it (as David Gatti mentions in his answer, it is an object after all). Secondly, certain characters - including backslashes - will need to be escaped. When you parse it back into an object, the additional backslashes will be removed.
Your corrected JSON should read:
{
"equations": [
"$w = F.s\\cos\\theta$ "
]
}
JSON is an encoding of structured data. You write
{
"equations": [
"$w = F.s\\cos\\theta$"
]
}
to mean an object with a property named equations with an array with a single string:
$w = F.s\cos\theta$
The escaped backslashes (\) does not change the underlying data. They get removed by the receiver when the JSON gets decoded into the object graph.
I am trying to make sure input from user is converted into a valid JSON string before submitted to server.
What I mean by 'Converting' is escaping characters such as '\n' and '"'.
Btw, I am taking user input from HTML textarea.
Converting user input to a valid JSON string is very important for me as it will be posted to the server and sent back to client in JSON format. (Invalid JSON string will make whole response invalid)
If User entered
Hello New World,
My Name is "Wonderful".
in HTML <textarea>,
var content = $("textarea").val();
content will contain new-line character and double quotes character.
It's not a problem for server and database to handle and store data.
My problem occurs when the server sends back the data posted by clients to them in JSON format as they were posted.
Let me clarify it further by giving some example of my server's response.
It's a JSON response and looks like this
{ "code": 0, "id": 1, "content": "USER_POSTED_CONTENT" }
If USER_POSTED_CONTENT contains new-line character '\n', double quotes or any characters that are must be escaped but not escaped, then it is no longer a valid JSON string and client's JavaScript engine cannot parse data.
So I am trying to make sure client is submitting valid JSON string.
This is what I came up with after doing some researches.
String.prototype.escapeForJson = function() {
return this
.replace(/\b/g, "")
.replace(/\f/g, "")
.replace(/\\/g, "\\")
.replace(/\"/g, "\\\"")
.replace(/\t/g, "\\t")
.replace(/\r/g, "\\r")
.replace(/\n/g, "\\n")
.replace(/\u2028/g, "\\u2028")
.replace(/\u2029/g, "\\u2029");
};
I use this function to escape all the characters that need to be escaped in order to create a valid JSON string.
var content = txt.val().escapeForJson();
$.ajax(
...
data:{ "content": content }
...
);
But then... it seems like str = JSON.stringify(str); does the same job!
However, after reading what JSON.stringify is really for, I am just confused. It says JSON.stringify is to convert JSON Object into string.
I am not really converting JSON Object to string.
So my question is...
Is it totally ok to use JSON.stringify to convert user input to valid JSON string object??
UPDATES:
JSON.stringify(content) worked good but it added double quotes in the beginning and in the end. And I had to manually remove it for my needs.
Yep, it is totally ok.
You do not need to re-invent what does exist already, and your code will be more useable for another developer.
EDIT:
You might want to use object instead a simple string because you would like to send some other information.
For example, you might want to send the content of another input which will be developed later.
You should not use stringify is the target browser is IE7 or lesser without adding json2.js.
I don't think JSON.stringify does what you need. Check the out the behavior when handling some of your cases:
JSON.stringify('\n\rhello\n')
*desired : "\\n\\rhello\\n"
*actual : "\n\rhello\n"
JSON.stringify('\b\rhello\n')
*desired : "\\rhello\\n"
*actual : "\b\rhello\n"
JSON.stringify('\b\f\b\f\b\f')
*desired : ""
*actual : ""\b\f\b\f\b\f""
The stringify function returns a valid JSON string. A valid JSON string does not require these characters to be escaped.
The question is... Do you just need valid JSON strings? Or do you need valid JSON strings AND escaped characters? If the former: use stringify, if the latter: use stringify, and then use your function on top of it.
Highly relevant: How to escape a JSON string containing newline characters using javascript?
Complexity. I don't know what say.
Take the urlencode function from your function list and kick it around a bit.
<?php
$textdata = $_POST['textdata'];
///// Try without this one line and json encoding tanks
$textdata = urlencode($textdata);
/******* textarea data slides into JSON string because JSON is designed to hold urlencoded strings ******/
$json_string = json_encode($textdata);
//////////// decode just for kicks and used decoded for the form
$mydata = json_decode($json_string, "true");
/// url decode
$mydata = urldecode($mydata['textdata']);
?>
<html>
<form action="" method="post">
<textarea name="textdata"><?php echo $mydata; ?></textarea>
<input type="submit">
</html>
Same thing can be done in Javascript to store textarea data in local storage. Again textarea will fail unless all the unix formatting is deal with. The answer is take urldecode/urlencode and kick it around.
I believe that urlencode on the server side will be a C wrapped function that iterates the char array once verses running a snippet of interpreted code.
The text area returned will be exactly what was entered with zero chance of upsetting a wyswyg editor or basic HTML5 textarea which could use a combination of HTML/CSS, DOS, Apple and Unix depending on what text is cut/pasted.
The down votes are hilarious and show an obvious lack of knowledge. You only need to ask yourself, if this data were file contents or some other array of lines, how would you pass this data in a URL? JSON.stringify is okay but url encoding works best in a client/server ajax.
I have json on my page coming from the string property of the model:
var myJson = '[{\"A\":1,\"B\":10,\"C\":\"214.53599548339844\",\"D\":\"72.52798461914062\"},
{\"A\":1,\"B\":11,\"C\":\"214.53599548339844\",\"D\":\"72.52798461914062\"}]'
I want to process that json via javascript on the page
I am doing $.parseJSON(#Html.Raw(Json.Encode(myJason))); but json still contain \" symbol. If i do $.parseJSON(#Html.Raw(Json.Decode(myJason))); it is just producing an $.parseJSON(System.Web.Helpers.DynamicJsonArray); How can I fix that?
Take your JSON and .stringify() it. Then use the .replace() method and replace all occurrences of ("\").
var myString = JSON.stringify(myJson);
var myNewString = myString.replace(/\\/g, "");
Hope this helps.
There are two ways
1 from where you get the JSON asked them to send you as url encoded format. at your end you need to decode url and you will get the Perfect JSON.
Other wise do the laborious replace method for each and every special charecter with respective char.
like above example you need to use replace("\","");
There is no JSON parser that will be able to deal with a JSON string that isn't properly formatted in the first place.
so you need to make sure that your theModel is formatted appropriately and according JSON.org standards.
Like
Koushik say you can use String operation
Hello im working on a project were i have to update a database filled with JSON data. However this data looks like this:
{\"Id\":\"1\",\"Sounding\":\"1.075\",\"Ullage\":\"0\",\"Full\":\"100\",\"CapacityM\":\"3.918\",\"CapacityMT\":\"3.918\",\"LCG\":\"3.779\",\"TCG\":\"0\",\"VCG\":\"2.39\",\"FSM\":\"4.492\"}
However if i convert the original data to json format (from CSV file), it appears like this:
{"ID":1,"SOUNDING":1.075,"ULLAGE":0.000,"FULL":100.000,"CAPACITY":3.918,"CAPACITY":3.918,"LCG":3.779,"TCG":0.000,"VCG":2.390,"FSM
":0.000}
How do i add all those slashes like in the first one so it is the correct format? Do I really need them?
Looks like the string in database is javascript (or C- or Java-, you should check) string escaped. Simplest way to do that is use org.apache.commons.lang3.StringEscapeUtils.escapeEcmaScript() or org.apache.commons.lang.StringEscapeUtils.escapeJavaScript() method. Also check the other methods of these classes.
Alternative would be to store the first JSON as string in 2nd temp JSON object, then convert the 2nd JSON object to string, then get substring containing just the escaped string value of 1st JSON object out of that (I think that would be the part between the 3rd and last double quotes in the 2nd JSON string...). Slightly hacky, but avoids adding extra library.
That looks like the data has been escaped for some programming language's string/text literal format.
If I have not misunderstood anything I think you can solve this using json_parse.
Here is an example: http://jsfiddle.net/mqchen/yEJdq/
json_parse(data, function(key, value) {
var floatVal = parseFloat(value);
return !isNaN(floatVal) && isFinite(value) ? floatVal : value;
});
It uses json_parse but sends it a delegate which interprets strings that look like numbers as numbers.
EDIT: here is a version which also converts all keys to uppercase (in case you also want that): http://jsfiddle.net/mqchen/yEJdq/2/