1

I have an autocomplete function :-

  $(document).ready(function () {
  $('input.autocomplete').autocomplete({

    data: {

The data JSON object will be responsible for giving the associated autocomplete values, the JSON key will be the autocomplete suggestions, and the JSON value will be null.

I don't want to manually enter values inside this JSON data like:-

data:{
    "Chocolate": null, 
    "Cake": null, 
    "Icecream": null, 
    "Pudding": null }

Instead, I want to fetch the JSON values from a text file, which will have these contents, so that I can just load the text file for this data JSON object, instead of having to manually enter it.

So, the list.txt file will have:-

           "Chocolate": null, 
            "Cake": null, 
            "Icecream": null, 
            "Pudding": null,
             "Cream:null 

And I want to call this list.txt file inside JSON object data, something like:-

data: {
$.get('list.txt') }

How can I achieve this?

3
  • seems weird to use a txt file for JSON.... Commented Sep 14, 2017 at 15:52
  • Are you using a server-side technology such as PHP? Also -- won't you have to manually enter the values into the text file? Commented Sep 14, 2017 at 15:54
  • @JacobMattison Actually I am using a bash script to add the semicolons and null, in the text file I will just have the names, and the script will convert it into JSON type text. Commented Sep 14, 2017 at 16:34

1 Answer 1

1

Try the below code. Read data from text file with ajax call and convert it into json object. It will work, but slight modification is needed with your text file as follows.

{
"Chocolate": "null", 
"Cake": "null", 
"Icecream": "null", 
"Cream":"null"
}

And your code will be as follows.

$(document).ready(function() {
    $.ajax({
        url : "list.txt",
        dataType: "text",
        success : function (data) {
            $(".text").html(data);
            var jsonData = JSON.parse(data);
        }
    });
}); 

If you don't want to modify the existing text file the below code will work.

$(document).ready(function() {
        $.ajax({
            url : "list.txt",
            dataType: "text",
            success : function (data) {
                $(".text").html(data);
                var jsonData = JSON.parse("{"+data+"}");

                 $('input.autocomplete').autocomplete({
                     data:jsonData 
                 });  //to initialize autocomplete
            }
        });
    }); 
Sign up to request clarification or add additional context in comments.

7 Comments

Is this is your expected output?
Thank you. But it does not work, I think it actually needs to be put it inside the autcomplete function - $('input.autocomplete').autocomplete({ data:{
For that get json data from the text file and store in in a global variable then pass that data inside autocomplete function. Try that also.
Or just consume data from text file and convert it into json outside autocomplete function then inside the success of ajax define that auto complete function and assign the json data to autocomplete's data.
How can I consume data from text file, is it $.get('text.html')? converting JSON, is it using JSON.parse? Should I store this in a variable to call it inside success function?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.