How can you import data that has hex strings using Mongo import? - mongodb

I am using mongoimport to import a json file. In one of the fields there is potentially regex expressions that contain hex strings such as:
{ "field1" : "/\x01\x02\x03/" }
I am getting the following error "FailedToParse: Hex escape not supported"

Try using double escape, what you have fails JSON validation.
{ "field1": "/\\x01\\x02\\x03/" }
This passes validation at http://jsonlint.com/
You may have to create a special case in your code for setting and getting this value, there is also an escape notion of \u in JSON which I believe denotes a hex escape but I've never used it before.
From some research I found this handy bit of info and example.
// Store RegExp pattern as a string
// Double backslashes are required to put literal \ characters in the string
var jsonObject = { "regex": "^http:\\/\\/" };

Related

encoding and decoding base64.in aws appsync resolver

I have a resolver that receives an argument "nextToken" ($ctx.args.nextToken) which will be in base64 string. In the request mapping template, I need to convert nextToken from base64 string into ascii string. I know that Appsync has $util.base64Decode(String) : byte[] but this function gives back byte[] but I want back an ascii string.
In addition, I will need to create base64 string from ascii string in the response mapping template. Again Appsync provides a function $util.base64Encode( byte[] ) : String but I don't know how to change my ascii string into byte[].
Anybody has any idea how to handle both situations. Thanks in advance.
VTL template $util functions for base64 accept a string and return a string.
Example VTL template:
#set($base64 = $util.base64Encode("Hello World"))
{
"label": "$util.base64Decode($base64)"
}
Returns "Hello World"

CsvParser not working for missing double quotes

I have a messages in file like below and I am using com.univocity.parsers.csv.CsvParser to split the string based on delimiter(in this case its -)
1-bc-"name"-def-address
1-abc-"name-def-address
I create my CsvParser object like
private val settings = new CsvParserSettings()
settings.getFormat.setDelimiter('-')
settings.setIgnoreLeadingWhitespaces(true)
settings.setIgnoreTrailingWhitespaces(true)
settings.setReadInputOnSeparateThread(false)
settings.setNullValue("")
settings.setMaxCharsPerColumn(-1)
val parser = new CsvParser(settings)
and parse the input message like :
for (line <- Source.fromFile("path\\test.txt").getLines) {
println(parser.parseLine(line).toList)
}
and the output is:
List(1, bc, name, def, address)
List(1, abc, name-def-address)
If you see the output you can see that for 1st message the string was split properly however for second message it takes everything as a value after first double quote. Does anyone know why the behavior is like this and how can I get the desired output? I am reading every message as a string to it should simple treat a quote/double quote as a character.
Author of this library here. When the quote is found after your - delimiter, the parser will try to find a closing quote.
The easiest way around this is to make the parser simply ignore quotes with:
settings.getFormat().setQuote('\0');
Hope it helps.

Make scala ignore multiple quotes in input

How do I make scala ignore the quotes inside of a String?
e.g.
val line1 = "<row Id="85" PostTypeId="1""
I want <row Id="85" PostTypeId="1" to be considered as a single string. However scala outputs error thinking that "<row Id=" is a string and everything after it is not related
Thanks in advance
val line1 = """<row Id="85" PostTypeId="1""""
Note those triple quotes ("""blahblah""") - to parse the string without escaping.

Talend String handling convert "0.12900-" string to -0.12900 in float

Need help with the below conversion in Talend:
"0.12900-" string to -0.12900 in float via Tmap expression.
I am not well versed with Java hence the difficulty.
You could try something like this :
row1.column.contains("-")?Float.parseFloat( "-"+ StringHandling.LEFT(row1.column,row1.column.length()-1)):Float.parseFloat(row1.column)
Float.parseFloat allows you to convert a string to a float type.
StringHandling.LEFT gets the first characters of a string, here the total length-1.
Ternary operator controls if your string contains "-", otherwise you just have to parse the "-" symbol

How do I deal with commas when writing Objects to CSV in Swift?

There seem to be other answers the this on stack overflow but nothing that is specific to swift.
I am generating a CSV from an Site Object containing 3 properties
Struct SiteDetails {
var siteName:String?
var siteType: String?
var siteUrl: String?
}
The problem is that siteName may contain a comma so its making it really hard to convert back from CSV into a object when I read the CSV file back as some lines have 4 or more CSV elements.
Here is the code I am using to export to CSV:
func convertToCSV(sites: [SiteDetails]) -> String {
var siteAsCSV = ""
siteAsCSV.appendContentsOf("siteName,siteType,siteUrl\n")
for site in sites {
siteAsCSV.appendContentsOf("\(site.siteName),\(site.siteType),\(site.siteUrl)\n")
}
}
Any ideas how to stop this extra comma issue?
The CSV specification suggests to wrap all fields containing special characters in double quotes.
I managed to get this working using the modification of adding the double quotes to each field. In Swift, this requires you to escape the quotation mark which looks like its not going to run when you look at Xcode's syntax highlighting, but it works fine.
func convertToCSV(sites: [SiteDetails]) -> String {
var SiteAsCSV = ""
SiteAsCSV.appendContentsOf("siteName,siteType,siteUrl\n")
for site in sites {
SiteAsCSV.appendContentsOf("\"\(site.SiteName)\",\"\(site.Sitetype)\",\"\(site.SiteUrl)\"\n")
}
}