How to use JSON.parse method?

Discussion in 'Javascript' started by DL, Nov 26, 2009.

  1. DL

    DL Guest

    MSDN has an interesting example. However, what I'd like to do is to
    evaluate if the data set is JSON compliant. For instance,
    var jdata = "name: don; sex: m";
    var notJdata = "<nuts... sjkdks klsd, name, sie jsjd, sex,
    ksdksk.....">

    Attempt to use it like the following produces nothing
    If (JSON.parse(jdata))
    {
    alert ('jason data')
    };

    How am I supposed to use it? Or if not using JSON.parse, what is
    already available within the javascript language?

    Thanks.
    DL, Nov 26, 2009
    #1
    1. Advertising

  2. On Nov 26, 8:17 pm, DL wrote:
    > MSDN has an interesting example. However, what I'd like
    > to do is to evaluate if the data set is JSON compliant.
    > For instance,
    > var jdata = "name: don; sex: m";


    That is not JSON data (it needs a set of brace characters around the
    contents).

    > var notJdata = "<nuts... sjkdks klsd, name, sie jsjd, sex,
    > ksdksk.....">


    Even without the carriage return, that is a syntax error, so not
    javascript source code at all.

    > Attempt to use it like the following produces nothing
    > If (JSON.parse(jdata))

    ^^
    An attempt to call an undefined/undeclared function named - If - will
    generate a runtime error, and generating an error is not producing
    "nothing". But as the - jdata - defined above was not a JSON string
    the outcome without the above syntax error and the runtime error here
    would probably still qualify as your "nothing".

    > {


    Styles of code formatting get argued about quite a bit, with very
    little of significance to back up any particular style (that is, it is
    mostly (somebody's) personal preference). I prefer not putting a
    carriage return before the opening brace here (so that it would have
    been on the end of the previous line). If that style had been used
    here, using - If - in place of - if - would have resulted in a syntax
    error instead of a runtime error. This would have been preferable as
    you get informed of that when the code is loaded, while a runtime
    error only gets triggered in the event that execution encounters this
    particular code.

    For anyone wondering; it was javascript's automatic semicolon
    insertion that prevented this from being a syntax error. The carriage
    return before the opening brace gave the automatic semicolon insertion
    system an opportunity to inset a semicolon turning the above into the
    syntax for a function call followed by a Block statement.

    > alert ('jason data')
    >
    > };
    >
    > How am I supposed to use it?


    What does the documentation say?

    > Or if not using JSON.parse, what is
    > already available within the javascript language?


    Either the code here does not show what you are actually doing, or
    explains why it does not 'work', independently of the JSON.parse
    behaviour, so suggesting what else may allow you to do whatever it is
    you are attempting to do is not practical.

    Richard.
    Richard Cornford, Nov 27, 2009
    #2
    1. Advertising

  3. DL

    DL Guest

    On Nov 27, 9:33 am, Richard Cornford <>
    wrote:
    > On Nov 26, 8:17 pm, DL wrote:
    >
    > > MSDN has an interesting example.  However, what I'd like
    > > to do is to evaluate if the data set is JSON compliant.
    > > For instance,
    > > var jdata = "name: don; sex: m";

    >
    > That is not JSON data (it needs a set of brace characters around the
    > contents).
    >
    > > var notJdata = "<nuts... sjkdks klsd, name, sie jsjd, sex,
    > > ksdksk.....">

    >
    > Even without the carriage return, that is a syntax error, so not
    > javascript source code at all.
    >
    > > Attempt to use it like the following produces nothing
    > > If (JSON.parse(jdata))

    >
    >   ^^
    > An attempt to call an undefined/undeclared function named - If - will
    > generate a runtime error, and generating an error is not producing
    > "nothing". But as the - jdata - defined above was not a JSON string
    > the outcome without the above syntax error and the runtime error here
    > would probably still qualify as your "nothing".
    >
    > >  {

    >
    > Styles of code formatting get argued about quite a bit, with very
    > little of significance to back up any particular style (that is, it is
    > mostly (somebody's) personal preference). I prefer not putting a
    > carriage return before the opening brace here (so that it would have
    > been on the end of the previous line). If that style had been used
    > here, using - If - in place of - if - would have resulted in a syntax
    > error instead of a runtime error. This would have been preferable as
    > you get informed of that when the code is loaded, while a runtime
    > error only gets triggered in the event that execution encounters this
    > particular code.
    >
    > For anyone wondering; it was javascript's automatic semicolon
    > insertion that prevented this from being a syntax error. The carriage
    > return before the opening brace gave the automatic semicolon insertion
    > system an opportunity to inset a semicolon turning the above into the
    > syntax for a function call followed by a Block statement.
    >
    > >  alert ('jason data')

    >
    > > };

    >
    > > How am I supposed to use it?

    >
    > What does the documentation say?
    >
    > > Or if not using JSON.parse, what is
    > > already available within the javascript language?

    >
    > Either the code here does not show what you are actually doing, or
    > explains why it does not 'work', independently of the JSON.parse
    > behaviour, so suggesting what else may allow you to do whatever it is
    > you are attempting to do is not practical.
    >
    > Richard.


    Thank you both, I've taken another route.
    DL, Nov 28, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    227
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    316
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    241
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    255
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    153
    David Karr
    Jun 17, 2013
Loading...

Share This Page