Need to strip JSON result before submitting to JQuery Autocomplete

I am using JQuery UI autocomplete to display some options taken from a JSON structure. I have a very simple stripped down example working here:
If you type "te" in the form you will see a couple of options appear.

This is all working fine and the script loads the values from the following file:

However in my actual implementation the JSON structure is generated within a page which contains full HTML structural tags and I need to somehow strip this out and only extract the JSON structure.
If you view source you will see what I mean.
If you try adding "te" in the above form you will see a parse error.

What I have done is to surround the JSON structure with container strings:
### START ###
### END ###

Ideally I would like to parse the output by splitting it to extract the contents.

So I am looking to do something like this:
var theresult=jsonoutput.split("### START ###")[1].split("### END ###")[0];

Somehow the above needs to be done before it is used by autocomplete. I hope this makes sense.

I am willing to look at other ways to achieve this. If I have to first grab the JSON structure into a variable, parse it then put it back into the autocomplete then so be it.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

replace :
                    success: function( data ) {
                        response( $.map( data.products, function( item ) {

                    success: function( data ) {
                        data = $.parseJSON(data.replace(/^### START ###\s|\s### END ###$/g,""));
                        response( $.map( data.products, function( item ) {
mike99cAuthor Commented:
Thanks leakin91, I applied your change here:

Unfortunately I still get the parse error. I am not sure if the syntax is actually correct.
mike99cAuthor Commented:
I managed to get it to work by trying a slightly different approach:

The changes I did are as follows:

1. Changed dataType to be "text" instead of "json"
2. Read the resulting text data then extract the JSON structure using the split method
3. Parse the JSON structure and copy back into the data variable.

I think the reason the solution given by leakim91 did not work was because autocomplete was expecting the data source to be correctly formatted JSON so just trying to load it would fail as it would not parse as a JSON structure. Hence we needed to load it as text to bypass any JSON parsing then extract the JSON string before parsing it as JSON.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

1. yes, I was thinking you already receivng text...
2. split, why not... the regex do the same in one sh
3. yes, this what I used

the issue with my answer was the type:json instead text
mike99cAuthor Commented:
OK just for the record I took leakim971's solution and replaced the dataType with "text".

There is no parse error but nothing appears which suggests the regular expression may be the issue.
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Just to add something to this.... Maybe a more elegant way, rather than adding the START and END tags and doing splits and/or regex, is to use jQuery to parse the HTML itself. So for the above example, remove the START and END tags from the generated HTML wrapped JSON, and then use this line to extract the "data"...
data = $.parseJSON(  $(data).find('body').html()  );

Open in new window

Obviously in your non-stripped down version, the JSON data may be located in a different place within the HTML content, but I'm sure you can work out how to change the above to select whatever is the correct element that holds your JSON.
mike99cAuthor Commented:
Thanks mccarl. I have implemented your solution by creating a new data file here:

If you view source I have removed the container strings.

I then loaded this file from this page and updated the script as you suggeted:

If you try it now and type "te" the data value becomes null after trying to extract the html. I don't understand why it is not working as all appears to be correct.
mccarlIT Business Systems Analyst / Software DeveloperCommented:
If you try it now and type "te" the data value becomes null after trying to extract the html.
My apologies, I wasn't able to easily test what I had written above, and it appears the jQuery (or more likely the browsers DOM manipulation) ignores some of the tags when parsing the HTML (like <html> <head> <body> etc).

However in my actual implementation the JSON structure is generated within a page which contains full HTML structural tags and I need to somehow strip this out and only extract the JSON structure.
If you try the code that I gave above with your ACTUAL full HTML structure, but just changing the jQuery selector expression from 'body' to whatever will select the HTML element that contains your JSON, you should find that it just works. It doesn't work in this simplified version that you are testing with because, as stated above, the <body> element is being ignored and so jQuery can't select it.

eg. I tried an example like this...
<!doctype html>
<meta charset="utf-8">
    <p>{"products":[{"ID":100,"ReferenceID":"TST001","Name":"Product 1"},{"ID":101,"ReferenceID":"TST002","Name":"Product 2"},{"ID":102,"ReferenceID":"TST003","Name":"Product 3"}]}</p>

Open in new window

... with the following line ...
data = $.parseJSON(  $(data).find('p').html()  );

Open in new window

... and it worked perfectly fine!
data = $.parseJSON( data.match(/({.*})/).pop() );
mike99cAuthor Commented:
The original response from leakim971 which although incorrect did in fact point in the right direction as I needed to intercept the data parameter before extracting the JSON structure before parsing it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.