ruby json new line break rendered format incorrectly - ruby-on-rails

i tried to rendor json format in ruby on rails
My code:(ruby code)
:address=> 'Address:\nshollinganallur,\nchennai.'
Rendered json output:
"address": "Address:\\nshollinganallur,\\nchennai."
I tried with old questions and answer. but nothing happend. Any help?

There are no newlines in this:
:address=> 'Address:\nshollinganallur,\nchennai.'
The \n escape sequence doesn't work in single quoted strings so you're just getting the two characters \ and n.
When that's converted to JSON, the \ has a meaning so it has to be escaped with another \. Hence the output you're seeing.
If you start with:
:address => "Address:\nshollinganallur,\nchennai."
then you'll get your newlines all the way through.

Related

Validate and clean up JSON in Omnis Studio?

A correct JSON is
[{
"user_id": 1,
"user_name": "John Doe I"
}, {
"user_id": 2,
"user_name": "Jane Doe III"
}]
But if it contains some illegal characters it will not validate. Like this with a CR after Doe:
[{"user_id":1,"user_name":"John Doe
I"},{"user_id":2,"user_name":"Jane Doe III"}]
My question is if there is a "clean up" function in Omnis Studio 8 where the output is a correct JSON?
EDIT
To replace or delete KNOWN characters is easy. The problem is that text copied from MS Word and the Web can contain UNKNOWN characters. So I am searching for a command like
Calculate VALIDJSON as keepvalidchar(NOTVALIDJSON)
Is there such a beast?
according to this post How do I handle newlines in JSON?
if you want newlines in a text string, you need to escape the \n. so \n in a the original json string should be valid replacement for a 'cr'. eg.
[{"user_id":1,"user_name":"John Doe\nI"},{"user_id":2,"user_name":"Jane Doe III"}]
My read on this is that if you are pulling the data out of a database to send some place on the web, you might want to escape the newline to retain it in the text, rather than altering the JSON after the fact. Of course, that depends on the purpose of the API call to the server receiving the json.
I guess the crux is
if you want the json to reflect the original text content including special characters, you escape it
if you don't, then you remove it. I'd probably clean the original source since I might want to leave the json formatted and doing a replace of \n for blank .. might not have the effect you want.
Are you simply looking for a
do replaceall(lcVar,kCR,'\n') Returns lcVar
function call?
Thus replacing the CR character with the escaped normal encoding of '\n'
I guess the other question is, are you creating the JSON to send, or receiving the JSON and trying to decode it?
If receiving, maybe OJSON.$formatjson() may help?

Escaping regex in a Ruby awk system command

The following works directly in my Mac OS X terminal, creating a file with a few lines:
awk '!/^1499\||^1598\||^1599\||^1999\||^2298\||^2299\||^2403\|/' "#{working_path}" > "#{filtered_file_path}"
However, when I attempt to use it in Ruby on Rails using backticks, the resulting file is empty:
`awk '!/^1499\||^1598\||^1599\||^1999\||^2298\||^2299\||^2403\|/' "#{working_path}" > "#{filtered_file_path}"`
An awk with a simple regex works. For example:
`awk '!/SMITH/' "#{working_path}" > "#{filtered_file_path}"`
So, the issue appears to be with the escaped pipe characters.
Ideas?
Some background I should have provided:
The file I am processing is pipe-delimited. I am filtering out lines with certain codes that are in the first value on the line. So, the regex I am using is something like ^2298\|.
The other pipes in the expression in single quotes are regex OR operators.
"working_path" and "filtered_file_path" are Ruby variables.
I just figured it out. The backslash that is escaping the pipe characters also needs to be escaped. Not sure why there is a difference between the regular Terminal and Ruby, but there it is. The working version:
`awk '!/^1499\\||^1598\\||^1599\\||^1999\\||^2298\\||^2299\\||^2403\\|/' "#{working_path}" > "#{filtered_file_path}"`
After challenging my assumption that the problem was Ruby on Rails, the accepted answer here is what explained it:
Pipe symbol | in AWK field delimiter

Is "\" an unfinished string? Why can't I use print("\")?

The question is simple. I just tried to print the \ character using this code:
print("\")
But an error has occurred saying that it's an unfinished string.
I'm using Lua 5.2 version.
Backslashes are used for escape sequences, you should print 2 backslashes, like this:
print("\\")
See this for more details on escaping strings, and just strings in general.

How to define a ruby array that contains a backslash("\") character?

I want to define an array in ruby in following manner
A = ["\"]
I am stuck here for hours now. Tried several possible combinations of single and double quotes, forward and backward slashes. Alas !!
I have seen this link as well : here
But couldn't understand how to resolve my problem.
Apart from this what I need to do is -
1. Read a file character by character (which I managed to do !)
2. This file contains a "\" character
3. I want to do something if my array A includes this backslash
A.includes?("\")
Any help appreciated !
There are some characters which are special and need to be escaped.
Like when you define a string
str = " this is test string \
and this contains multiline data \
do you understand the backslash meaning here \
it is being used to denote the continuation of line"
In a string defined in a double quotes "", if you need to have a double quote how would you doo that? "\"", this is why when you put a backslash in a string you are telling interpretor you are going to use some special characters and which are escaped by backslash. So when you read a "\" from a file it will be read as "\" this into a ruby string.
char = "\\"
char.length # => 1
I hope this helps ;)
Your issue is not with Array, your question really involves escape sequences for special characters in strings. As the \ character is special, you need to first prepend it (escape it) with a leading backslash, like so.
"\\"
You should also re-read your link and the section on escape sequences.
You can escape backslash with a backslash in double quotes like:
["\\"].include?("\\")

How does FasterCSV determine whether or not to add quote?

When I try to output some data into a text file using FasterCSV, sometimes it adds the quotes to the concatenated string and sometimes it does not.
For instance:
FasterCSV.generate do |csv|
csv << ["E"+company_code]
csv << ["A"+company_name]
end
Both company_code and company_name are Strings and contains data but the output will show:
EtheCompanyCode
"AtheCompanyName"
I found how to force quoting in FasterCSV's docs but I need exactly the opposite and can not figure out why it quotes one line and not the other when they are both strings...
If anybody has the solution, I'll be deeply grateful for a lead :)
Thanks
If the real input is 'theCompanyName' and 'theCompanyCode' then I would also be confused by one line being quoted and the other not. But I suspect your real input is something else.
Most likely, the quoted line has some character that needs quoting, such as a comma; while the unquoted line doesn't. (Other characters that typically need quoting in Excel-style CSVs are quotation marks and newlines.)

Resources