none
Text content from a file populated is a specific formatted string RRS feed

  • Question

  • Hi -

    I have a file with IP's and domain names with nothing else in the file.

    For each item in my file, I want to create string that looks likes the follow:

    "[\"1.1.1.1\",\"2.2.2.2\",\"3.3.3.3\",\"8.8.8.8\",\"google.com\"]"

    Here is how it looks in my file:

    filesampl.txt
    1.1.1.1
    2.2.2.2
    3.3.3.3
    8.8.8.8
    google.com

    I have been searching for few days on a solution and keep hitting road blocks. Appreciate the help in advance.

    Thanks
    Keith

    Sunday, January 19, 2020 7:51 PM

Answers

  • Allow me to read between the lines, because your question could mean a couple of things.  What you have shown as your target is a JSON string.  Is your goal to take your text file and convert it into JSON format?  That's easy, and there are two ways to do it.

    One way is to use simple strings and do the conversion yourself.  I assume you know or can figure out how to read your file one line at a time.  You can construct your string by something like:

        string json = "[";
        foreach( line in the file )
        {
            if( this is not the first line )
                json = json + ",";
            json = json + "\"" + line + "\"";
        }
        json = json + "]";

    You can probably do better with a StringBuilder, but that's the basic idea.

    The other, and arguably more "correct" way of doing it, is to read the lines of your file into a string array, and then use the JSON serializer to convert that to a JSON string.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Sunday, January 19, 2020 8:23 PM

All replies

  • Allow me to read between the lines, because your question could mean a couple of things.  What you have shown as your target is a JSON string.  Is your goal to take your text file and convert it into JSON format?  That's easy, and there are two ways to do it.

    One way is to use simple strings and do the conversion yourself.  I assume you know or can figure out how to read your file one line at a time.  You can construct your string by something like:

        string json = "[";
        foreach( line in the file )
        {
            if( this is not the first line )
                json = json + ",";
            json = json + "\"" + line + "\"";
        }
        json = json + "]";

    You can probably do better with a StringBuilder, but that's the basic idea.

    The other, and arguably more "correct" way of doing it, is to read the lines of your file into a string array, and then use the JSON serializer to convert that to a JSON string.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Sunday, January 19, 2020 8:23 PM

  • I have a file with IP's and domain names with nothing else in the file.

    Here is how it looks in my file:

    filesampl.txt
    1.1.1.1
    2.2.2.2
    3.3.3.3
    8.8.8.8
    google.com


    You have left out important details.

    (1) Are there multiple entries in the file which follow the *exact same* format 
        you have shown?

    (2) If so, are there always *exactly* four lines with the IP strings followed 
        by one line with a domain name?
        
    - Wayne

    Sunday, January 19, 2020 8:54 PM
  • Some files have thousands and it is random when it is an IP or domain name.

    Thanks

    Sunday, January 19, 2020 9:19 PM
  • Some files have thousands and it is random when it is an IP or domain name.

    "thousands" of what? Lines? "Items"?

    Your original post said: "For each item in my file".

    How then do you define an "item"?

    Are we to understand that you want [ before the first *line* in the file and ]
    after the last *line* in the file? Or what?

    You have to be *very* specific and precise in your problem description.
    We can't suggest code solutions based on assumptions or vague generalizations.

    - Wayne

    Sunday, January 19, 2020 10:02 PM
  • Using the example from Tim, I figured it out. 

    Now I have a JSON error that I need to research. One step at a time.

    I appreciate all the help. Thank you
    Keith

    Sunday, January 19, 2020 10:39 PM
  • Hi Keith,

    It seems that your problem has been solved. If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 20, 2020 6:48 AM
  • Done and thank you - I fixed my JSON error now to just figure out the last item in the list and it should work. Again - thank you
    Monday, January 20, 2020 1:48 PM