Trying to remove quotes from arrayed JSON keys - javascript

I have this array of javascript objects (which are actually musical notes) that are generated in my javascript front-end. In order to store it, I had to run javascript JSON.stringify(objectArray)before throwing it into a hidden input, but that automatically encases all my keys in double quotes like so:
[
{"class":"barline","symbol":"standard","barline":true,"newSystem":true},
{"class":"note","rhythm":"half","duration":0.5,"symbol":"flag","hand":"R","newbar":true,"rebel":false},
{"class":"note","rhythm":"half","duration":0.5,"symbol":"flag","hand":"R","endbar":true,"rebel":false},
{"class":"barline","symbol":"standard","barline":true},
{"class":"note","rhythm":"half","duration":0.5,"symbol":"flag","hand":"R","newbar":true,"rebel":false}
]
Before filtering my params in Rails, I run JSON.parse(params[:score][:notes]) to turn it from a string into a proper JSON array for storage in MongoDB (I'm specifically using Mongoid)
I know it's normally proper procedure to keep keys in quotes in most cases, but I like using the dot notation for keys to grab values in all my JS.
Should I switch my JS to reference everything with brackets, or can you think of a simple function that would quickly parse out the keys' quotes before sending to the hidden input?

I was looking to do something similar to pass linting, and this regex finds all quotes before semicolons in the string:
"(\w*)":
And replace that with just the text, excluding the quotes:
$1:

Related

Convert invalid JSON (having equal sign instead of colon, no quotes etc) to valid JSON

I am getting data from Cardstream payment gateway android sdk (in native module) to react native but the data is not a valid JSON:
'{ __wafRequestID=2021-06-16T08:02:14Z|0e2314f32f|115.186.169.10|gk0GHP1i4V, action=SALE, addressCheckPref=not known,not checked,matched,not matched,partially matched, amount=14, amountRetained=0, avscv2CheckEnabled=Y, caEnabled=Y, cardCVVMandatory=Y, cardExpiryDate=0322, cardFlags=8323072, cardIssuer=UNKNOWN, cardIssuerCountry=United Kingdom, cardIssuerCountryCode=GBR, cardNumberMask=424242******4242, cardNumberValid=Y, cardScheme=Visa, cardSchemeCode=VC, cardType=Visa Credit, cardTypeCode=VC, cftEnabled=N, countryCode=826, currencyCode=826, currencyExponent=2, customerName=fgg, customerReceiptsRequired=Y, cv2CheckPref=not known,not checked,matched,not matched,partially matched, eReceiptsEnabled=N, merchantAlias=100001, merchantID=100001, merchantID2=100001, paymentMethod=card, postcodeCheckPref=not known,not checked,matched,not matched,partially matched, processMerchantID=100001, requestID=60c9b007225c7, requestMerchantID=100001, responseCode=65566, responseMessage=Disallowed cardnumber, responseStatus=2, riskCheckEnabled=Y, riskCheckPref=not known=continue,not checked=continue,approve=continue,decline=decline1,review=authonly,escalate=authonly, riskProcessorID=41, riskProcessorName=Kount, rtsEnabled=Y, scaExemption=lowvalue,trusted, state=finished, surchargeEnabled=Y, surchargeRequired=Y, threeDSCheckPref=authenticated, threeDSEnabled=N, timestamp=2021-06-16 09:02:16, transactionID=112446674, type=1, vcsResponseCode=0, vcsResponseMessage=Success, xref=21061609KX02TQ16YP35TTD}'
How to convert it to valid JSON so that I can parse it? I have tried few regex replacements but some values also have colons which is messing it up.
If you wanted to try an parse this with regex then this might work:
[{,] (?<key>[a-zA-Z0-9_]+)=(?<value>.*?(?=, [a-zA-Z0-9_]+=|\}$))
https://regex101.com/r/Fk7NvR/1
Just loop through the matches and access the captured groups named key and value respectively. The value for riskCheckPref seems suspect but I have no clue about their parsing rules.
An alternate idea would be to remove the outer curly braces, trim white space, split on , (comma space), and split again on the first =.
I converted response to JSON in android and then passed it to react-native instead of passing response to react-native (which gets passed in this invalid format) and then banging my head to convert it to JSON. My bad. Thanks all

How can I convert a large list of numbers into JavaScript array format?

I have this large list of numbers: http://www.codeshare.io/MGpQ4, that I would like to convert into a JavaScript array format, so that I can check if a certain number in a function matches one of the numbers in that repsective array. Is there any tool I can use to convert all these numbers into a JS array format?
try to split this list. You need to add something like coma or space at the end of your lines.
string.split(/* your delimetter */);
string here is your list in vaid JS format.
If you show real code, i could answer better.
There are many ways to do this, but basically the idea is to replace line breaks by ,. Any IDE that supports regex replacement will allow you to do this easily. You can also use virtually any programming language and use the same approach.
Here's your array literal:
[11450,3082,163222,57748,176966,163225,18786,163224,175522,175523,176967,176968,163226,147285,139762,147317,57793,57784,147324,147333,147336,163220,147337,147338,147339,147340,147341,147342,147343,147192,163175,163351,57847,147202,147211,147214,147215,147284,147216,152398,147218,147219,147220,147221,163397,163424,163425,163399,163427,163400,57749,163401,163402,57875,1070,171146,11452,77475,51933,156790,51920,15615,84544,153564,99754,51946,57916,51919,175521,51973,28016,176957,15620,1067,57929,57881,57882,57883,57885,57884,175524,122321,131411,122320,57886,175525,57887,122322,156789,3075,11462,51581,77411,175528,152719,51580,156801,51568,15648,51583,152553,99735,153797,51582,152554,51567,175529,51586,28017,15652,51587,57974,15628,57917,57918,57919,57920,15630,175526,175527,15629,57927,150956,122340,122341,122342,15633,11467,155198,175658,155201,175654,175655,175656,175697,175657,155200,175653,155199,175652,155203,155202,175640,163147,175648,90635,80913,80914,52762,53369,86207,175649,19255,175650,163129,125474,175651,125473,163130,125475,163131,163132,155344,175641,175642,175643,155345,155346,175644,175645,175646,155348,163148,155349,116724,175647,155347,155350,155351,82161,163149,155240,155241,155242,155243,163141,155244,155245,163142,163143,112424,163144,163145,155246,155247,155248,155249,155250,155251,155252,155253,163146,155254,112425,175638,152360,152361,152362,152363,163139,152364,152365,152366,163140,150666,152367,152368,152369,112428,172007,152345,152352,163137,152353,152354,152355,163138,152356,152357,152358,152359,112426,175639,150663,152340,152341,150664,152342,153004,4250,52357,86647,2993,152537,177649,2994,175626,167902,52365,169271,105860,105408,163582,163584,169272,52373,163583,15665,52382,179239,79720,179194,179240,179241,179242,105387,15662,90634,2996,106113,175627,1060,1059,57990,57991,15687,137084,3002,57988,11483,57989,15689,11510,11511,3001,11484,155183,15690,11507,15691,84434,313,93427,15709,11498,24087,53120,11501,11504,11505,63850,28015,175628,178962,163526,175629,105417,163525,105418,105419,105422,105440,105432,105444,16119,175630,163528,155184,155185,163537,163542,155187,177650,169277,169278,163543,169279,169280,169281,169282,178963,163549,169284,163550,166695,179243,155189,179195,179244,179245,179246,155190,169283,169276,155191,155192,166696,155195,166697,155194,155193,155197,155196,167905,3259,15720,105471,106127,163556,163552,163553,15718,98735,163560,163551,98745,175631,105516,15719,105472,106128,175633,131474,131476,105508,105509,105511,105512,105514,105515,148486,114293,105520,3263,175632,1265,4251,163573,3003,177651,168998,105559,45220,167906,45230,169285,105544,169286,169287,169288,169289,45237,15735,45238,150955,179247,45246,179196,179248,179249,179250,151486,105569,45258,175634,175635,175636,175637,163581,106129,15738,1063,15724,137085,63862,63861,11524,163587,11525,163588,11527,11523,11514,63853,163593,11521,11522,163586,63854,11530,63855,11532,45279,15746,4844,11554,3009,169001,172378,63856,11534,11535,53447,46809,15751,11517,15752,15755,15753,167903,117769,63857,15747,11536,117771,108898,11538,155206,15748,63859,63858,15754,1264,63863,11555,63864,63865,63866,155226,63867,63869,53159,15775,84275,314,169291,63852,169292,169293,169295,80600,169296,169298,169302,42526,163570,169303,3034,95672,53557,45333,55793,53548,62107,11632,63889,175759,175760,163600,163601,163602,163603,163604,163605,20287,11681,163606,163607,175805,20288,175806,74962,74963,74964,74965,74966,74967,74968,167909,962,163608,163609,167910,163610,163611,167911,163612,163613,167912,163614,163615,163616,163617,4116,14062,163618,163619,163620,163621,163622,163623,163624,163625,163626,14065,175807,163627,48559,14070,1464,74969,74970,74971,74972,74973,74974,74975,167913,960,957,163594,163595,163596,163597,163598,163599,167908,20291,175761,52383,52384,167914,52385,52386,52387,52388,155227,52389,52390,167915,52391,52392,52393,52394,155228,52395,52396,167916,52397,52401,52398,52399,155229,52400,52402,167917,52403,52404,52405,155230,52406,155231,52407,106130,48861,163628,163629,163630,48862,28022,175762,48864,48865,52408,52409,48866,52410,52411,52412,52413,155232,52414,52415,52416,52417,48867,52418,52419,52420,52421,52422,52423,52424,52425,52426,48891,52427,155233,48908,52438,52428,48881,48868,52429,167918,52430,52431,52432,52433,52434,52435,52436,52437,48886,167919,48892,48897,48902,155235,48909,52439,52440,48882,48869,52441,48887,166698,52442,52443,52444,52445,52446,52447,52448,52449,167920,48893,48898,48903,155236,48910,52450,52451,48883,48870,52452,48888,166699,52453,52454,52455,52457,52458,52459,52460,52461,167921,48894,48899,48904,155238,48911,155239,106131,965,74976,74977,74978,74979,74980,74981,74982,167922,48561,125472,156949,156950,125476,155234,125477,155237,156952,156951,170065,91247,41964,45051,41965,45052,45053,50975,50977,50976,52535,163631,41966,45054,50978,50979,50980,45055,50981,50982,50983,50985,50986,52536,50987,52537,50988,50989,50990,50991,50992,50993,50994,41967,45056,45057,14879]
It seems that you plan to use your array to check the existence of some values. In this case, you might be better off with an object map which will perform O(1) lookups.
{"313":true,"314":true,"957":true,"960":true,"962":true,"965":true,"1059":true,"1060":true,"1063":true,"1067":true,"1070":true,"1264":true,"1265":true,"1464":true,"2993":true,"2994":true,"2996":true,"3001":true,"3002":true,"3003":true,"3009":true,"3034":true,"3075":true,"3082":true,"3259":true,"3263":true,"4116":true,"4250":true,"4251":true,"4844":true,"11450":true,"11452":true,"11462":true,"11467":true,"11483":true,"11484":true,"11498":true,"11501":true,"11504":true,"11505":true,"11507":true,"11510":true,"11511":true,"11514":true,"11517":true,"11521":true,"11522":true,"11523":true,"11524":true,"11525":true,"11527":true,"11530":true,"11532":true,"11534":true,"11535":true,"11536":true,"11538":true,"11554":true,"11555":true,"11632":true,"11681":true,"14062":true,"14065":true,"14070":true,"14879":true,"15615":true,"15620":true,"15628":true,"15629":true,"15630":true,"15633":true,"15648":true,"15652":true,"15662":true,"15665":true,"15687":true,"15689":true,"15690":true,"15691":true,"15709":true,"15718":true,"15719":true,"15720":true,"15724":true,"15735":true,"15738":true,"15746":true,"15747":true,"15748":true,"15751":true,"15752":true,"15753":true,"15754":true,"15755":true,"15775":true,"16119":true,"18786":true,"19255":true,"20287":true,"20288":true,"20291":true,"24087":true,"28015":true,"28016":true,"28017":true,"28022":true,"41964":true,"41965":true,"41966":true,"41967":true,"42526":true,"45051":true,"45052":true,"45053":true,"45054":true,"45055":true,"45056":true,"45057":true,"45220":true,"45230":true,"45237":true,"45238":true,"45246":true,"45258":true,"45279":true,"45333":true,"46809":true,"48559":true,"48561":true,"48861":true,"48862":true,"48864":true,"48865":true,"48866":true,"48867":true,"48868":true,"48869":true,"48870":true,"48881":true,"48882":true,"48883":true,"48886":true,"48887":true,"48888":true,"48891":true,"48892":true,"48893":true,"48894":true,"48897":true,"48898":true,"48899":true,"48902":true,"48903":true,"48904":true,"48908":true,"48909":true,"48910":true,"48911":true,"50975":true,"50976":true,"50977":true,"50978":true,"50979":true,"50980":true,"50981":true,"50982":true,"50983":true,"50985":true,"50986":true,"50987":true,"50988":true,"50989":true,"50990":true,"50991":true,"50992":true,"50993":true,"50994":true,"51567":true,"51568":true,"51580":true,"51581":true,"51582":true,"51583":true,"51586":true,"51587":true,"51919":true,"51920":true,"51933":true,"51946":true,"51973":true,"52357":true,"52365":true,"52373":true,"52382":true,"52383":true,"52384":true,"52385":true,"52386":true,"52387":true,"52388":true,"52389":true,"52390":true,"52391":true,"52392":true,"52393":true,"52394":true,"52395":true,"52396":true,"52397":true,"52398":true,"52399":true,"52400":true,"52401":true,"52402":true,"52403":true,"52404":true,"52405":true,"52406":true,"52407":true,"52408":true,"52409":true,"52410":true,"52411":true,"52412":true,"52413":true,"52414":true,"52415":true,"52416":true,"52417":true,"52418":true,"52419":true,"52420":true,"52421":true,"52422":true,"52423":true,"52424":true,"52425":true,"52426":true,"52427":true,"52428":true,"52429":true,"52430":true,"52431":true,"52432":true,"52433":true,"52434":true,"52435":true,"52436":true,"52437":true,"52438":true,"52439":true,"52440":true,"52441":true,"52442":true,"52443":true,"52444":true,"52445":true,"52446":true,"52447":true,"52448":true,"52449":true,"52450":true,"52451":true,"52452":true,"52453":true,"52454":true,"52455":true,"52457":true,"52458":true,"52459":true,"52460":true,"52461":true,"52535":true,"52536":true,"52537":true,"52762":true,"53120":true,"53159":true,"53369":true,"53447":true,"53548":true,"53557":true,"55793":true,"57748":true,"57749":true,"57784":true,"57793":true,"57847":true,"57875":true,"57881":true,"57882":true,"57883":true,"57884":true,"57885":true,"57886":true,"57887":true,"57916":true,"57917":true,"57918":true,"57919":true,"57920":true,"57927":true,"57929":true,"57974":true,"57988":true,"57989":true,"57990":true,"57991":true,"62107":true,"63850":true,"63852":true,"63853":true,"63854":true,"63855":true,"63856":true,"63857":true,"63858":true,"63859":true,"63861":true,"63862":true,"63863":true,"63864":true,"63865":true,"63866":true,"63867":true,"63869":true,"63889":true,"74962":true,"74963":true,"74964":true,"74965":true,"74966":true,"74967":true,"74968":true,"74969":true,"74970":true,"74971":true,"74972":true,"74973":true,"74974":true,"74975":true,"74976":true,"74977":true,"74978":true,"74979":true,"74980":true,"74981":true,"74982":true,"77411":true,"77475":true,"79720":true,"80600":true,"80913":true,"80914":true,"82161":true,"84275":true,"84434":true,"84544":true,"86207":true,"86647":true,"90634":true,"90635":true,"91247":true,"93427":true,"95672":true,"98735":true,"98745":true,"99735":true,"99754":true,"105387":true,"105408":true,"105417":true,"105418":true,"105419":true,"105422":true,"105432":true,"105440":true,"105444":true,"105471":true,"105472":true,"105508":true,"105509":true,"105511":true,"105512":true,"105514":true,"105515":true,"105516":true,"105520":true,"105544":true,"105559":true,"105569":true,"105860":true,"106113":true,"106127":true,"106128":true,"106129":true,"106130":true,"106131":true,"108898":true,"112424":true,"112425":true,"112426":true,"112428":true,"114293":true,"116724":true,"117769":true,"117771":true,"122320":true,"122321":true,"122322":true,"122340":true,"122341":true,"122342":true,"125472":true,"125473":true,"125474":true,"125475":true,"125476":true,"125477":true,"131411":true,"131474":true,"131476":true,"137084":true,"137085":true,"139762":true,"147192":true,"147202":true,"147211":true,"147214":true,"147215":true,"147216":true,"147218":true,"147219":true,"147220":true,"147221":true,"147284":true,"147285":true,"147317":true,"147324":true,"147333":true,"147336":true,"147337":true,"147338":true,"147339":true,"147340":true,"147341":true,"147342":true,"147343":true,"148486":true,"150663":true,"150664":true,"150666":true,"150955":true,"150956":true,"151486":true,"152340":true,"152341":true,"152342":true,"152345":true,"152352":true,"152353":true,"152354":true,"152355":true,"152356":true,"152357":true,"152358":true,"152359":true,"152360":true,"152361":true,"152362":true,"152363":true,"152364":true,"152365":true,"152366":true,"152367":true,"152368":true,"152369":true,"152398":true,"152537":true,"152553":true,"152554":true,"152719":true,"153004":true,"153564":true,"153797":true,"155183":true,"155184":true,"155185":true,"155187":true,"155189":true,"155190":true,"155191":true,"155192":true,"155193":true,"155194":true,"155195":true,"155196":true,"155197":true,"155198":true,"155199":true,"155200":true,"155201":true,"155202":true,"155203":true,"155206":true,"155226":true,"155227":true,"155228":true,"155229":true,"155230":true,"155231":true,"155232":true,"155233":true,"155234":true,"155235":true,"155236":true,"155237":true,"155238":true,"155239":true,"155240":true,"155241":true,"155242":true,"155243":true,"155244":true,"155245":true,"155246":true,"155247":true,"155248":true,"155249":true,"155250":true,"155251":true,"155252":true,"155253":true,"155254":true,"155344":true,"155345":true,"155346":true,"155347":true,"155348":true,"155349":true,"155350":true,"155351":true,"156789":true,"156790":true,"156801":true,"156949":true,"156950":true,"156951":true,"156952":true,"163129":true,"163130":true,"163131":true,"163132":true,"163137":true,"163138":true,"163139":true,"163140":true,"163141":true,"163142":true,"163143":true,"163144":true,"163145":true,"163146":true,"163147":true,"163148":true,"163149":true,"163175":true,"163220":true,"163222":true,"163224":true,"163225":true,"163226":true,"163351":true,"163397":true,"163399":true,"163400":true,"163401":true,"163402":true,"163424":true,"163425":true,"163427":true,"163525":true,"163526":true,"163528":true,"163537":true,"163542":true,"163543":true,"163549":true,"163550":true,"163551":true,"163552":true,"163553":true,"163556":true,"163560":true,"163570":true,"163573":true,"163581":true,"163582":true,"163583":true,"163584":true,"163586":true,"163587":true,"163588":true,"163593":true,"163594":true,"163595":true,"163596":true,"163597":true,"163598":true,"163599":true,"163600":true,"163601":true,"163602":true,"163603":true,"163604":true,"163605":true,"163606":true,"163607":true,"163608":true,"163609":true,"163610":true,"163611":true,"163612":true,"163613":true,"163614":true,"163615":true,"163616":true,"163617":true,"163618":true,"163619":true,"163620":true,"163621":true,"163622":true,"163623":true,"163624":true,"163625":true,"163626":true,"163627":true,"163628":true,"163629":true,"163630":true,"163631":true,"166695":true,"166696":true,"166697":true,"166698":true,"166699":true,"167902":true,"167903":true,"167905":true,"167906":true,"167908":true,"167909":true,"167910":true,"167911":true,"167912":true,"167913":true,"167914":true,"167915":true,"167916":true,"167917":true,"167918":true,"167919":true,"167920":true,"167921":true,"167922":true,"168998":true,"169001":true,"169271":true,"169272":true,"169276":true,"169277":true,"169278":true,"169279":true,"169280":true,"169281":true,"169282":true,"169283":true,"169284":true,"169285":true,"169286":true,"169287":true,"169288":true,"169289":true,"169291":true,"169292":true,"169293":true,"169295":true,"169296":true,"169298":true,"169302":true,"169303":true,"170065":true,"171146":true,"172007":true,"172378":true,"175521":true,"175522":true,"175523":true,"175524":true,"175525":true,"175526":true,"175527":true,"175528":true,"175529":true,"175626":true,"175627":true,"175628":true,"175629":true,"175630":true,"175631":true,"175632":true,"175633":true,"175634":true,"175635":true,"175636":true,"175637":true,"175638":true,"175639":true,"175640":true,"175641":true,"175642":true,"175643":true,"175644":true,"175645":true,"175646":true,"175647":true,"175648":true,"175649":true,"175650":true,"175651":true,"175652":true,"175653":true,"175654":true,"175655":true,"175656":true,"175657":true,"175658":true,"175697":true,"175759":true,"175760":true,"175761":true,"175762":true,"175805":true,"175806":true,"175807":true,"176957":true,"176966":true,"176967":true,"176968":true,"177649":true,"177650":true,"177651":true,"178962":true,"178963":true,"179194":true,"179195":true,"179196":true,"179239":true,"179240":true,"179241":true,"179242":true,"179243":true,"179244":true,"179245":true,"179246":true,"179247":true,"179248":true,"179249":true,"179250":true}
Like plalx said, any IDE with regex replacement can do it.
As an alternative, you can chech http://www.regexr.com/
And use something like /([0-9]+)/g for matching those numbers and then use a substitution like $1,
Then just add your [].
In your Excel spreadsheet, create a cell with a function that pulls all the numbers in the list into a javascript array format. Something like:
=CONCATENATE("[", TRANSPOSE(A1:A700 & ”, “) ,"]")
Something like that should create a new cell with all your list values in a single, comma delimited string, enclosed by [].
Copy the entire string and place it in your javascript file as a variable.
var list = [value1, value2, value3, ...];
Then you can do whatever you want with it.

JavaScript string declarations from dynamic data with unescaped quotation marks

I am writing JavaScript templates for a content management system where users fill out text input fields that are passed to my templates.
My problem is the quotation marks in the input fields are not escaped before they are passed to my template, so I have no way of knowing if they will contain single or double quotes, or even both. Whichever way I try to handle the data my code ends up breaking because the quotes terminate the string declaration. I want to run a function on the data to escape quotes but I can't find a way to get the data into a valid variable first.
Is there any way to safely handle the data in JavaScript without it breaking a string variable declaration?
Edit: I'm posting code example;
CMS Text Input Field value is: Who'll win our "Big Contest"?
Text Input Field placeholder macro is [%TextInput%]
I'm building an HTML template for this input, using just JS/HTML/CSS
<script>
(function(){
var textInputStr = "[%TextInput%]";
})();
</script>
This will break the string declaration if the value of TextInput contains a single quote, and vice versa.
This is an awesome question, and one that deserves an answer. Strings in JS don't have a custom delimiter, like in most other modern languages, so you can get really stuck. But one solution is to build a script tag with the placeholder inside it, then find the innerHTML of that tag and grab the string back into a variable. eg
<script id="parseMe" type="text/template">
[%TextInput%]
</script>
then use
var yourString = document.getElementById("parseMe").innerHTML
Now you can manipulate the string as you please.
HTH!
I want to run a function on the data to escape quotes but I can't find a way to get the data into a valid variable first.
Well, you will have to make it a valid string literal before you could run JavaScript functions on it. There's no other way (unless you count an ajax request to the template script to get a string representation of it).
The input fields are not escaped before they are passed to my template
Then fix that. There's nothing you can do about it in JavaScript.

How to retain escape sequences while extracting strings from json objects in javascript. (Strings are actually regex patterns)

I have a JSON object as follows:
var jsonObject = {"regex":"<span class=\"Value\">\\$(.+?)<\\/span>"};
My target is to use this regular expression to scrape a value from a html document.
var match = html.match(new RegExp(jsonObject.regex, 'i'));
This however returns an error. The problem seems to be that the escape sequences in the regex string are lost in the string jsonObject.regex
A call to jsonObject.regex returns
< span class="Value">\$(.+?)<\ /span>
(The escape sequences like \" and \\ are lost)
I could replace the respective characters using javascript, but it seems the inefficient thing to do since I already have the correct format in the json object.
Any clues or workarounds are appreciated. Thanks!
You are doing two things wrong here.
First and foremost, you are trying to build a program that uses arbitrary regular expressions on HTML. Don't do that. You have a DOM at your disposal on the client side, you should use one of the selector engines available. Examples include the browser built-in document.querySelectorAll(), Sizzle (which is also part of jQuery), NWMatcher, or an XPath-based selector engine like XPath.js.
Then, you obviously do not use a JSON serializer to build your JSON string on the server side, or things like messed-up escaping would not happen on the client side.
Lastly, what you have in your first code sample is not JSON. It's a JavaScript object literal. JSON is always a string:
'{"regex":"<span class=\"Value\">\\$(.+?)<\\/span>"}'
Selecting what you seem to want in jQuery would become as simple as
var value = $("span.value").text();
But as I said, you are not bound to use jQuery, there are lighter-weight alternatives if HTML-scraping is your main goal.

Django json single and double quotes?

I noticed that if you use the simplejson function in django all the strings are enclosed with single quotes, and the whole json object string is enclosed in double quotes. When I take this string and hand it in to JSON.parse, it gives me an error because they want to use single quotes to enclose the whole object and double quotes for the strings. I could switch them with javascript replace, but then I'd have to take into consideration cases like apostrophes, but I'm sure there is a better way. Is there a way to get django's simplejson to output the object string to the format of JSON.parse?
More info:
django view:
def view(request):
list = [{"a":"apple",},]
return HttpResponse(simplejson.dumps(str(list)), mimetype="application/json")
what the javascript string turn out to be
"[{'a': 'apple'}]"
update
remove the str() around list, simply simplejson.dumps(list). str() trans the list to a string, thus you got "[{'a': 'apple'}]" in client side.
Can you update the question to demo where simplejson encloses strings w/ single quotes?
django.utils.simplejson, normally, conforms w/ JSON specification and does not use single quotes to wrap things. If you mean
>>> from django.utils.simplejson import dumps
>>> dumps("Hello")
'"Hello"' # single quotes here
>>> repr(dumps("Hello"))
'\'"Hello"\'' # or here
They're notations of Python, you don't want to directly use them in JSON.parse (the first one is OK though).

Categories