Javascript Array with Double Quotes - nativescript

I'm using Nativescript Http.getString to get a list of dates from a web service.
The Web service returns a string as expected which I then want to use in a Javascript Array.
The String is "'31/08/2021','24/12/2021','25/12/2021','26/12/2021','27/12/2021','28/12/2021','29/12/2021','30/12/2021','31/12/2021','01/01/2022','01/04/2022','04/04/2022'" or similar, the dates will change over time.
But I want to have a Javascript DateArray = ['31/08/2021','24/12/2021','25/12/2021','26/12/2021','27/12/2021','28/12/2021','29/12/2021','30/12/2021','31/12/2021','01/01/2022','01/04/2022','04/04/2022'] so removing the double quotes before using in the array.
I cannot find a way to remove the double quotes and apply the result to the Javascript Array.

Ok so I've rewritten to output JSON then parse the JSON output to a Javascript Array. Much easier to do and handle rather than a string output.

Related

JMeter CompoundVariable.execute method is replacing + character with empty space

I'm trying to substitute a value into a JSON request body using a JMeter variable and ${value_here} notation. The value is a base64 encoded image, which includes "+" characters.
When I call CompoundVariable.execute, the request body contains the value in the JMeter variable, but all "+" characters have been replaced with empty strings resulting in a malformed image.
Is there some workaround for this, or do I need to work around it in code? Simplified example before, since I am sure none of you want the wall of text that would be my encoded image.
String stored in variable (truncated for brevity):
/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCADDASsDASIAAhEBAxEB/8QAHQAAAQQDAQEAAAAAAAAAAAAABQAEBgcBAgMICf/EAEkQAAEDAwMBBAYECwYGAgMBAAECAwQABREGEiExBxNBURQVImFxgTJUkZIIIzM0QnSTobGy4SREYnPB0RZSY4KD8BhyF1NkhP/EABkBAAMBAQEAAAAAAAAAAAAAAAABAgMEBf/EACgRAAICAgICAQMEAwAAAAAAAAABAhESMQMhQVETInGhMkJhsVKR8P/aAAwDAQACEQMRAD8A9U1o44hsZcWlI8ycU1uk1NvgvSXOdieE+Z8B86BItwlOF+8K9KeVyGl/Qb9yU/60CboPi4Q+f7Uz98Vn1hD+tM/fFBRbIRORCYx4juxxSTbYPUwo3zQKdCyDXrCH9aZ++KXp8P60z98UG9WQfqUbn/piserYPP8AYY5A8e7FFBkGvT4f1pn74penw/rTP3xQY22ABlUOMP8AxjmserYOfzKMf+wUUGQa9YQ/rTP3xS9YQ/rTP3xQU26Dn80jEe5sVk22DwBCj8/4BRQZBn1hD+tM/fFL1hD+tM/
Variable in templated request is ${Document_Image_Front} though I'm sure that is irrelevant.
You can use __urlencode function to encode space to + back
${__urldecode(Word "school" is "école" in french)}
returns Word+%22school%22+is+%22%C3%A9cole%22+in+french.

Liquid - parse YAML front matter in string

I'm running a Jekyll site that uses JSON as data in my _data folder. I'm looping through the file like normal doing things like {% for item in site.data.resources.items %} just fine. However, I'd like to parse YAML front matter that is within a string.
Example:
\n---\nblog: http://google.com\nbackground-img: http://www.ew.com/sites/default/files/i/2013/07/23/Dumb-and-Dumber.jpg\nbuttonText: Download\n---\n
How can I have Liquid parse this within my Jekyll site so I can use it like so:
<img src="{{background-img}}>Image
or something similar?
EDIT: To clarify, that string is in front matter format in a text file that I'm retrieving through an ajax call. So that string is the response I get back and the format won't be changing. My hope was that Liquid could somehow parse this string and look for a front matter type format. If not, I will revert back my JavaScript methods.
This is impossible.
Liquid/YML is being parsed while generating the site and your JSON string comes available long after the site has been generated: It only exists after the moment the JSON request for the string has been succesful.
However, you can use javascript, as you already mentioned. Simply split the string on \n for your key-value pairs and split on : for your key and value. Then use jQuery (or plain javascript) to write the results to the DOM.
Good luck!

Removing brackets in my API request using Ruby?

I have an API script written in ruby that is returning data. The variable being used to return data is being passed includes brackets and quotes ["likeso"] resulting in no data being returned because it is expecting it to be only: likeso
Any ideas how to solve this problem?
I think when you parse the json string you will get only likeso
It's showing when you print the output.

ExpressionEngine template will not output empty JSON array

I'm creating JSON in an ExpressionEngine template and pointing the Ruby JSON library at the relevant URL. The template looks like this:
[
{exp:mylib:mytag channel="mychannel" backspace="1"}
{"entry_id":"{entry_id}","title":"{title}"},
{/exp:mylib:mytag}
]
When the tag returns data, everything is fine, my Ruby code works perfectly with the array of objects. However, when the tag returns no data (because there are no appropriate entries), Ruby complains that the json string is not the required 2 octets in length. I would expect the output to be [], i.e. an empty but valid JSON array. However, visiting the URL in Firefox/firebug and wget confirms that the response coming back from the URL is zero bytes in length, with status 200 OK.
I tested further by creating a template without tags and just a pair of empty square brackets, with the same result: zero bytes.
Is a pair of empty square brackets somehow a reserved token in the EE template language? Is there some clever optimisation going on that assumes that no-one could ever want a pair of square brackets in an html page?
Are you developing your own add-on, or using the built-in ExpressionEngine tags?
Using the native channel entries queries, you can use a if_no_results conditional tag to control what gets output when there are no matching results:
{exp:channel:entries channel="channel_name"}
{if no_results} ...{/if}
{/exp:channel:entries}
Many third-party add-ons also support the same type of {if_no_results} conditional.
You might also have a look at the third-party ExpressionEngine JSON add-on, which may be able to give you some inspiration on how to approach your situation.

What's the correct way to get HTML from an AJAX Response in JSON Format?

I have an AJAX request that creates a 'post', and upon successful post, I want to get HTML to inject back into the DOM. Right now I'm returning a JSON array that details success/error, and when I have a success I also include the HTML for the post in the response. So, I parse the response as JSON, and set a key in the JSON array to a bunch of HTML Code.
Naturally, the HTML code is making the JSON array break -- what should I do to escape it (or is there a better way to do this?). I get an AJAX response with a JSON array like so:
[{response:"success"},{html:'<div class="this is going to break...
Thanks!
Contrary to what you're probably used to in JavaScript, ' can't begin a string in JSON. It's strictly a ". Single quotes work when you're passing JSON to JavaScript.. much like <br> works when you want to put an XHTML line break.
So, use " to open the HTML string, and sanitize your quotes with \".
json.org has more info WRT what you should sanitize. Though the list of special characters isn't long, it's probably best to use a library like Anurag suggests in a comment.
Apart from escaping double quotes as mention by BranTheMan, newlines also break JSON strings. You need to replace newlines with \n.
Personally I've found this to be enough:
// Don't know what your serverside language is, example in javascript syntax:
print(encodeJSON({
response : "success",
html : htmlString.replace(/\n/g,'\\n').replace(/"/g,'\\"')
}));

Resources