Read string which contains \" from mongodb - c++

In my MongoDB, I have stored below string
"description" : "25\"",
But when I try to read it in C++ driver using both ways below, I always get "25""
d->description=record.getStringField("description");
or
d->description = record.getField("description").jsonString(Strict);
I need to keep back slash \ here, because the string will be sent to web browser, JavaScript code will parse this string to JSON object.
Any way to do this?

Not sure how that string got in there, but this will not serialize or deserialize properly without the proper escaping. It should look more like this:
{ "description" : "25\\\"" }
You should update these with your driver, which should do the serialization properly just based on your regular input, ie 25".
When the fields in the document look like above then they will deserialize how you want.

Related

Regular expression to extract either integer or string from JSON

I am working in an environment without a JSON parser, so I am using regular expressions to parse some JSON. The value I'm looking to isolate may be either a string or an integer.
For instance
Entry1
{"Product_ID":455233, "Product_Name":"Entry One"}
Entry2
{"Product_ID":"455233-5", "Product_Name":"Entry One"}
I have been attempting to create a single regex pattern to extract the Product_ID whether it is a string or an integer.
I can successfully extract both results with separate patterns using look around with either (?<=Product_ID":")(.*?)(?=") or (?<=Product_ID":)(.*?)(?=,)
however since I don't know which one I will need ahead of time I would like a one size fits all.
I have tried to use [^"] in the pattern however I just cant seem to piece it together
I expect to receive 455233-5 and 455233 but currently I receive "455233-5"
(?<="Product_ID"\s*:\s*"?)[^"]+(?="?\s*,)
, try it here.

Need to extract a specific detail from a json

I have the following json which i'm looking to extract the specific string which come after location_code and between quotes XXX123
{"location_code":"XXX123","location_uuid":"XXX-XXX-XXAA-4444-ASDFSDAF44","hotstamp":"1111","card_format":"ABC","accesses":[{"partition_name":"SSSSuljiro","SSSSS":"3","access_levels":["ASDASDASDA"],"location_code":"XXX123"}]}
Would greatly appreciate any help with this!!
I'm using redshift and tried several attempts with regexp_substr
This should work:
(?<="location_code":")\w+
but since you want to parse a json object, there could be better/easier ways to do so.
I believe you could use json_extract_path_text:
select json_extract_path_text(
json_column, -- your json
'location_code', -- json key to extract data from
true -- return null if input is invalid json
);
Make sure that your string is actually valid JSON format.

QT5.7 - Why i get a malformed json value with QString but perfect with std::string?

I try to get a json response from an api in Ruby On Rails.
When I call this url directly with curl or postman I get a perfect json response.
When I use my program with QT5.7 windows compiled in Static for a program in 32bits, I get a perfect response only if use std::string.
But, if I use QDebug for print a QString() I get this malformed and Strange result :
"{\"success\":true,\"files\":[\"C:/Perl/lib/pods/perlcn.pod\",\"C:/Perl/lib/pods/perldata.pod\",\"C:/Perl/lib/pods/perldebguts.pod\",\"C:/Perl/lib/pods/perldelta.pod\",\"C:/Perl/lib/pods/perldiag.pod\",\"C:/Perl/lib/pods/perldoc.pod\",\"C:/Perl/lib/pods/perldos.pod\",\"C:/Perl/lib/pods/perldsc.pod\",\"C:/Perl/lib/pods/perldtrace.pod\",\"C:/Perl/lib/pods/perlebcdic.pod\",\"C:/Perl/lib/pods/perlembed.pod\",\"C:/Perl/lib/pods/perlexperiment.pod\",\"C:/Perl/lib/pods/perlfaq.pod\",\"C:/Perl/lib/pods/perlfaq1.pod\",\"C:/Perl/lib/pods/perlfaq2.pod\",\"C:/Perl/lib/pods/perlfaq3.pod\",\"C:/Perl/lib/pods/perlfaq4.pod\",\"C:/Perl/lib/pods/perlfaq5.pod\",\"C:/Perl/lib/pods/perlfaq6.pod\",\"C:/Perl/lib/pods/perlfaq7.pod\",\"C:/Perl/lib/pods/perlfaq8.pod\",\"C:/Perl/lib/pods/perlfaq9.pod\",\"C:/Perl/lib/pods/perlfilter.pod\",\"C:/Perl/lib/pods/perlfork.pod\",\"C:/Perl/lib/pods/perlform.pod\",\"C:/Perl/lib/pods/perlfreebsd.pod\",\"C:/Perl/lib/pods/perlfunc.pod\",\"C:/Perl/lib/pods/perlgit.pod\",\"C:/Perl/lib/pods/perlglossaƮv
So, if I print std::string, I have a perfect json, exactly what i want :
{"success":true,"files":["C:/Perl/lib/pods/perlcn.pod","C:/Perl/lib/pods/perldata.pod","C:/Perl/lib/pods/perldebguts.pod","C:/Perl/lib/pods/perldelta.pod","C:/Perl/lib/pods/perldiag.pod","C:/Perl/lib/pods/perldoc.pod","C:/Perl/lib/pods/perldos.pod","C:/Perl/lib/pods/perldsc.pod","C:/Perl/lib/pods/perldtrace.pod","C:/Perl/lib/pods/perlebcdic.pod","C:/Perl/lib/pods/perlembed.pod","C:/Perl/lib/pods/perlexperiment.pod","C:/Perl/lib/pods/perlfaq.pod","C:/Perl/lib/pods/perlfaq1.pod","C:/Perl/lib/pods/perlfaq2.pod","C:/Perl/lib/pods/perlfaq3.pod","C:/Perl/lib/pods/perlfaq4.pod","C:/Perl/lib/pods/perlfaq5.pod","C:/Perl/lib/pods/perlfaq6.pod","C:/Perl/lib/pods/perlfaq7.pod","C:/Perl/lib/pods/perlfaq8.pod","C:/Perl/lib/pods/perlfaq9.pod","C:/Perl/lib/pods/perlfilter.pod","C:/Perl/lib/pods/perlfork.pod","C:/Perl/lib/pods/perlform.pod","C:/Perl/lib/pods/perlfreebsd.pod","C:/Perl/lib/pods/perlfunc.pod","C:/Perl/lib/pods/perlgit.pod","C:/Perl/lib/pods/perlglossary.pod","C:/Perl/lib/pods/perlgpl.pod","C:/Perl/lib/pods/perlguts.pod","C:/Perl/lib/pods/perlhack.pod","C:/Perl/lib/pods/perlhacktips.pod","C:/Perl/lib/pods/perlhacktut.pod","C:/Perl/lib/pods/perlhaiku.pod","C:/Perl/lib/pods/perlhist.pod","C:/Perl/lib/pods/perlhpux.pod","C:/Perl/lib/pods/perlhurd.pod","C:/Perl/lib/pods/perlintern.pod","C:/Perl/lib/pods/perlinterp.pod","C:/Perl/lib/pods/perlintro.pod","C:/Perl/lib/pods/perliol.pod","C:/Perl/lib/pods/perlipc.pod","C:/Perl/lib/pods/perlirix.pod","C:/Perl/lib/pods/perljp.pod","C:/Perl/lib/pods/perlko.pod","C:/Perl/lib/pods/perllexwarn.pod","C:/Perl/lib/pods/perllinux.pod","C:/Perl/lib/pods/perllocale.pod","C:/Perl/lib/pods/perllol.pod","C:/Perl/lib/pods/perlmacos.pod","C:/Perl/lib/pods/perlmacosx.pod","C:/Perl/lib/pods/perlmod.pod","C:/Perl/lib/pods/perlmodinstall.pod","C:/Perl/lib/pods/perlmodlib.pod","C:/Perl/lib/pods/perlmodstyle.pod","C:/Perl/lib/pods/perlmroapi.pod","C:/Perl/lib/pods/perlnetware.pod","C:/Perl/lib/pods/perlnewmod.pod","C:/Perl/lib/pods/perlnumber.pod","C:/Perl/lib/pods/perlobj.pod","C:/Perl/lib/pods/perlootut.pod","C:/Perl/lib/pods/perlop.pod","C:/Perl/lib/pods/perlopenbsd.pod","C:/Perl/lib/pods/perlopentut.pod","C:/Perl/lib/pods/perlos2.pod","C:/Perl/lib/pods/perlos390.pod","C:/Perl/lib/pods/perlos400.pod","C:/Perl/lib/pods/perlpacktut.pod","C:/Perl/lib/pods/perlperf.pod","C:/Perl/lib/pods/perlplan9.pod","C:/Perl/lib/pods/perlpod.pod","C:/Perl/lib/pods/perlpodspec.pod","C:/Perl/lib/pods/perlpodstyle.pod","C:/Perl/lib/pods/perlpolicy.pod","C:/Perl/lib/pods/perlport.pod","C:/Perl/lib/pods/perlpragma.pod","C:/Perl/lib/pods/perlqnx.pod","C:/Perl/lib/pods/perlre.pod","C:/Perl/lib/pods/perlreapi.pod","C:/Perl/lib/pods/perlrebackslash.pod","C:/Perl/lib/pods/perlrecharclass.pod","C:/Perl/lib/pods/perlref.pod","C:/Perl/lib/pods/perlreftut.pod","C:/Perl/lib/pods/perlreguts.pod","C:/Perl/lib/pods/perlrepository.pod","C:/Perl/lib/pods/perlrequick.pod","C:/Perl/lib/pods/perlreref.pod","C:/Perl/lib/pods/perlretut.pod","C:/Perl/lib/pods/perlriscos.pod","C:/Perl/lib/pods/perlrun.pod","C:/Perl/lib/pods/perlsec.pod","C:/Perl/lib/pods/perlsolaris.pod","C:/Perl/lib/pods/perlsource.pod","C:/Perl/lib/pods/perlstyle.pod","C:/Perl/lib/pods/perlsub.pod","C:/Perl/lib/pods/perlsymbian.pod","C:/Perl/lib/pods/perlsyn.pod","C:/Perl/lib/pods/perlsynology.pod","C:/Perl/lib/pods/perlthrtut.pod"]}
I have no idea what i can do because i need to parse my json with QString for QJsonDocument and QJsonObject.
I have try many things like
QNetworkAccessManager
Or (ugly thing for understand and debug) like :
Curl external
Thanks
Are you using qDebug() for stdout output? This is not what it should be used for.
It displays, in debug format, the current contents of many types. For QString it means the string in quotes with certain character - double quote included - escaped with \. That doesn't mean the string itself contains escaped data. It's only presented to you like that by QDebug.

Regex to parse the "Accept" header

I'm working on a REST API. The client is using the Accept header in their request to send in stuff like
...application/vnd.mywebsite+json; version=1... or
...application/vnd.mywebsite+xml; version=2....
Currently, I am parsing the headers and picking out the media type and version to serve with string functions:
json and 1
xml and 2
I was wondering if I could do that faster with a regex.
How can I pull out the format and version from an "Accept" header in the request? I suppose, I would need to make 2 regex calls to get this done, and that's okay.
Update :
Using the answer below, I tried extracting those using ColdFusion, but the pattern just matches the whole string.
Ideally, I want an array of 2 elements, ie ['json', '1']. Any ideas ?
<cfscript>
arrTitles = reMatch(
"application/vnd.website\+([A-Za-z]+);\s*version=(\d+)",
"application/vnd.website+json; version=2"
);
writedump(arrTitles);
</cfscript>
Please refer this runnable example.
You could use something simple like this:
application/vnd.mywebsite\+([A-Za-z]+);\s*version=(\d+)
The type (json or xml) would be in capturing group 1, the version in group 2.
You can see it working here.

Coldfusion JSON Breaking with DataTables

Working on one of the tasks i am using jsstringformat function to handle json data if some special characters are used, but that does not seems to handle all issues.
My JSON still breaks.
I am using like this :
"<a href='edit.cfm?id=#jsStringFormat(qFiltered.randomnumber)#' style='color:##066D99'>#trim(jsStringFormat(qFiltered[thisColumn][qFiltered.currentRow]))#</a>"
I am lost here what else i can use as any part of regex or rereplace that it should not break
Thanks
You're doing multiple things here.
You're putting the string into a URL: use UrlEncodedFormat.
You're also putting it in an HTML tag: use HtmlEditFormat.
The whole thing is going into a JavaScript variable, so I would use JSStringFormat to wrap the whole thing.
Try building your string before assigning it.
<cfsavecontent variable="htmlLink"><cfoutput>
#HtmlEditFormat(Trim(qFiltered[thisColumn][qFiltered.currentRow]))#
</cfoutput></cfsavecontent>
myJsVar = "#JsStringFormat(Trim(htmlLink))#";