Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: JSON Notation

  1. #1
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts

    JSON Notation

    I'm trying to get a return of
    Code:
    [
       ["red","green","blue"]
       ,["blue","red","green"]
    ]
    from a text file that contains
    Code:
    red,green,blue
    blue,red,green
    Using the following function
    Quote Originally Posted by Old Pedant View Post
    Code:
    function WRITE_FILE_AS_JSON(file)
     {
      fso = Server.CreateObject("Scripting.FileSystemObject");
      myPath=Server.MapPath("timecards/" + file);
      if(!fso.FileExists(myPath))return;
      myfile = fso.OpenTextFile( myPath, read);
      var lines = myfile.readAll().split("\n");
      Response.Write("[\n");
      for ( var i = 0; i < lines.length; ++i )
      {
          Response.Write('   ' + (i >0 ? ',' : '' ) + '["' + lines(i).replace(/\,/g,'","') + '"]\n');
      }
      Response.Write("]\n");
      myfile.Close();
      Response.End();
     }
    The darn thing keeps returning
    500 (Internal Server) Error
    as a result of the line Response.Write(' ' + (i >0 ? ',' : '' ) + '["' + lines(i).replace(/\,/g,'","') + '"]\n');
    And I don't know how to fix that line...

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Is it possibly because the last line is null and the replace doesn't like to work on a null value?

    Maybe try this:
    Code:
      for ( var i = 0; i < lines.length; ++i )
      {
          var line = lines[i];
          if ( line != null && line.length > 1 && line.indexOf(",") > 0 )
          {
              Response.Write('   ' + (i >0 ? ',' : '' ) + '["' + line.replace(/\,/g,'","') + '"]\n');
          }
      }
    If that's not it, can you show a sample the text file(s) you are using? At least 3 or 4 lines of one? (Change names to protect the guilty of course.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    LMAO! Oh, too funny!

    I *INADVERTENTLY* fixed the *ACTUAL* bug!

    I did:
    Code:
          var line = lines[i];
    Note that the original code does
    Code:
       ... lines(i).replace(/\,/g,'","') ...
    LOOK AT THAT CAREFULLY!

    Now change it to
    Code:
       ... lines[i].replace(/\,/g,'","') ...
    Too much VBScript. Arrays in JS *always* are indexed via [...], never (...).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Mar 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post
    Use JSON.stringify, it's built into modern browsers. For IE6 and similar, you can get the script from json.org.

  • #5
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Oh, wow! I should've caught that... I don't even use VBScript! That should have stuck out like a sore thumb but I bet that fixes it. IDK, I guess I just thought you were going over my head again... Also, that was the text from the external file. I thought I would try that before complicating anything...

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Yeah, after I posted the first msg I noticed you said that was the text, so I decided to create a test using that text. That's when I noticed it. I was too blind to see that I'd changed the effective coding in my first post.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Coiner: The code he is showing there is SERVER-side JavaScript (or, technically, JScript). ASP allows you to use JScript as a scripting language. But since it is server-side, it has none of the browser-related capabilities (e.g., DOM interaction) nor does it have JSON.stringify.

    You missed the significance of this line:
    Code:
      fso = Server.CreateObject("Scripting.FileSystemObject");
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New Coder
    Join Date
    Mar 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post
    Well considering this is a client side javascript forum I assumed otherwise. Regardless, the json.org script will still work and should be used as it is already optimized to work with any javascript object.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Good point. No reason not to download and use it, even server side.

    But it would likely be slower and a lot more work for his simple case.

    After all, he would first have to convert the text file into a JavaScript object (admiittedly, trivial in this case) and then #include the entire json.org library (which will be big and have tons of unused code for this simple page), and then finally invoke the stringify.

    I don't see any advantage in that for this case. Can see it might be an advantage for more advanced objects.

    FWIW, the code would be something like this:
    Code:
    function WRITE_FILE_AS_JSON(file)
     {
      fso = Server.CreateObject("Scripting.FileSystemObject");
      myPath=Server.MapPath("timecards/" + file);
      if(!fso.FileExists(myPath))return;
      myfile = fso.OpenTextFile( myPath, read);
      var lines = myfile.readAll().split("\n");
      for ( var i = 0; i < lines.length; ++i )
      {
          lines[i] = lines[i].split(",");
      }
      Response.Write( JSON.stringify(lines) );
      myfile.Close();
      Response.End();
     }
    Yeah, that's even more trivial that I was thinking it would be. Still, the overhead of that #include may not be worth it. Closer call than I thought it would be, though.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Both excellent points, thank you.
    So then, what if that function was the beginning of a larger project; and I have been looking for a way to normalize the data aggregated from external files. Would you recommend converting my text files into "Javascript Object"'s and then include the entire json.org library (or perhaps a stripped down version enabling stringify) if the objects became more advance along the way, and how would this be accomplished? Or would that still be too bulky with "unused code" and best suited for other projects?
    @coiner I must apologize for posting this in the client side javascript forum. codingforums.com does not have a server side variant, and the asp forum seems only useful for VBScripting issues. While I have come to understand the distinction, the help provided here is usually ECMAScript compliant which is my primary concern. So the confusion was entirely my fault.

    Lastly, where would I find helpful tutorials regarding this library? (I'll be googling this myself, I was just wondering if you had any recommendations)

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    The #include overhead may not be an issue, at all. It depends on your site and your server load and and...

    ASP *does* cache the pre-compiled form of ASP pages. But it caches them in memory and uses an LRU algorithm for keeping cached pages around. So if (a) your site doesn't really have many pages in use or (b) this page is used a lot [keeping it hot in the cache], then the likelihood is that the pre-compiled form will be there when needed and then the only overhead there is consists of the extra memory used by the compiled JSON library. Alternatively, if your server is lightly loaded, then the extra time to compile JSON each time it is used isn't *that* much and so may not impact you much at all.

    And, yes, properly we should have discussed this in the ASP forum, but as you say, it's pretty much all VBS over there.

    Personally, I would probably go the JSON path if your goal is to use a unified object model both client and server side, which is not a bad goal at all.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •