1

I am trying to make a website for users to type information about themselves and eventually generate a Json file for them to download. I was able to create this using Java but I need some help turning this into a website for users to use.

Also, if there is any way I can do this using just JS, HTML and CSS.

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import org.json.simple.*;

public class JsonFile {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {

        @SuppressWarnings("resource")
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter ID: ");
        int id_Input = scan.nextInt();
        System.out.println("Enter First Name: ");
        String firstname_Input = scan.next();
        System.out.println("Enter Last Name: ");
        String lastname_Input = scan.next();

        JSONObject patient = new JSONObject();
        patient.put("id", id_Input);
        patient.put("firstName", firstname_Input);
        patient.put("lastName", lastname_Input);

        System.out.println("Enter Father's First Name: ");
        String firstname_father = scan.next();
        System.out.println("Enter Father's Last Name: ");
        String lastname_father = scan.next();

        JSONObject father = new JSONObject();
        father.put("firstName", firstname_father);
        father.put("lastName", lastname_father);

        JSONArray list = new JSONArray();
        list.add(patient);
        list.add(father);

        try(FileWriter file = new FileWriter("testJSON.json")) {
            file.write(list.toString());
            file.flush();
        }
       catch(IOException e) {
            e.printStackTrace();
       }
    }
}
2
  • 1
    If you take user input with <input> tags and assign their values to values. Then make an object in JS Ex: var jsonObj = {}; jsonObj.name = nameVar; jsonObj.age = ageVar; etc. Then using JSON.stringify(jsonObj), it will be converted to a string which you can then make a file for them to download. The reason I'm not putting this in an official answer is because I don't have time to test it myself right now. Commented Jul 11, 2018 at 19:52
  • Possible duplicate of Create a file in memory for user to download, not through server Commented Jul 11, 2018 at 20:07

1 Answer 1

2

According to my understanding you need to convert JSON based on user input. Use my below example code. May be it will help you.

   <html>
<input type="text" id="txtid" value="Enter id"/>
<input type="text" id="txtfirstname" value="Enter first name"/>
<input type="text" id="txtlastname" value="Enter last name"/>
<input type="button" value="Display" id="btnDisplay" onclick="output()"/>

    <script>
        function output(){
            //Data collector
            var oData=[];
            //Local Data object initialization 
            var local={
                id:document.getElementById("txtid").value,
                firstname:document.getElementById("txtfirstname").value,
                lastname:document.getElementById("txtlastname").value
            };
            //Push data in data collector
            oData.push(local);
            //Convert data in json format string
            var output=JSON.stringify(oData).toString();
            //Data output
           document.write("<h1>"+ output +"</h1>")

           download("testfile.txt",output)
        }

        function download(filename, text) {
            var element = document.createElement('a');
            element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
            element.setAttribute('download', filename);
            element.style.display = 'none';
            document.body.appendChild(element);
            element.click();
            document.body.removeChild(element);
        }

    </script>
</html>

//**Paste this code in HTML file and run**.

//This is simple example for your reference only.
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much @Abhishek Tomar I will keep you updated as I work more on this project! This code will definitely help me to get started :)
If this is helpful for you please accept my answers. Click on check in front of my post. Glad to help.
Just did ;) Thanks!

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.