Why does Python 3 json.loads(“foo.com”) not work yet json.loads(“123.6”) does?










2















When I try to load "foo.com" I get an exception, as expected; however, if I am loading a string containing a float number, it works. Just trying to figure out why this is the case.



$ python3
Python 3.6.5 (default, Jun 17 2018, 12:13:06)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> j = json.loads("foo.com")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
>>> j = json.loads("123.6")
>>>









share|improve this question






















  • Because 123.6 is valid JSON, but foo.com (without quotes!) is not?

    – ForceBru
    Nov 15 '18 at 21:20












  • @ForceBru Both are quoted, therefore both should be strings.

    – Franz Kafka
    Nov 15 '18 at 21:23











  • @DeepSpace, because the grammar doesn't allow it: tools.ietf.org/html/rfc7159#section-2

    – ForceBru
    Nov 15 '18 at 21:25











  • @FranzKafka, they're strings for Python, but not the JSON parser.

    – ForceBru
    Nov 15 '18 at 21:26











  • 123.6 is a valid number literal, "foo.com" is a valid string literal, but foo.com isn't valid json

    – mata
    Nov 15 '18 at 21:33















2















When I try to load "foo.com" I get an exception, as expected; however, if I am loading a string containing a float number, it works. Just trying to figure out why this is the case.



$ python3
Python 3.6.5 (default, Jun 17 2018, 12:13:06)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> j = json.loads("foo.com")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
>>> j = json.loads("123.6")
>>>









share|improve this question






















  • Because 123.6 is valid JSON, but foo.com (without quotes!) is not?

    – ForceBru
    Nov 15 '18 at 21:20












  • @ForceBru Both are quoted, therefore both should be strings.

    – Franz Kafka
    Nov 15 '18 at 21:23











  • @DeepSpace, because the grammar doesn't allow it: tools.ietf.org/html/rfc7159#section-2

    – ForceBru
    Nov 15 '18 at 21:25











  • @FranzKafka, they're strings for Python, but not the JSON parser.

    – ForceBru
    Nov 15 '18 at 21:26











  • 123.6 is a valid number literal, "foo.com" is a valid string literal, but foo.com isn't valid json

    – mata
    Nov 15 '18 at 21:33













2












2








2








When I try to load "foo.com" I get an exception, as expected; however, if I am loading a string containing a float number, it works. Just trying to figure out why this is the case.



$ python3
Python 3.6.5 (default, Jun 17 2018, 12:13:06)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> j = json.loads("foo.com")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
>>> j = json.loads("123.6")
>>>









share|improve this question














When I try to load "foo.com" I get an exception, as expected; however, if I am loading a string containing a float number, it works. Just trying to figure out why this is the case.



$ python3
Python 3.6.5 (default, Jun 17 2018, 12:13:06)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> j = json.loads("foo.com")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
>>> j = json.loads("123.6")
>>>






json python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 21:17









Franz KafkaFranz Kafka

149120




149120












  • Because 123.6 is valid JSON, but foo.com (without quotes!) is not?

    – ForceBru
    Nov 15 '18 at 21:20












  • @ForceBru Both are quoted, therefore both should be strings.

    – Franz Kafka
    Nov 15 '18 at 21:23











  • @DeepSpace, because the grammar doesn't allow it: tools.ietf.org/html/rfc7159#section-2

    – ForceBru
    Nov 15 '18 at 21:25











  • @FranzKafka, they're strings for Python, but not the JSON parser.

    – ForceBru
    Nov 15 '18 at 21:26











  • 123.6 is a valid number literal, "foo.com" is a valid string literal, but foo.com isn't valid json

    – mata
    Nov 15 '18 at 21:33

















  • Because 123.6 is valid JSON, but foo.com (without quotes!) is not?

    – ForceBru
    Nov 15 '18 at 21:20












  • @ForceBru Both are quoted, therefore both should be strings.

    – Franz Kafka
    Nov 15 '18 at 21:23











  • @DeepSpace, because the grammar doesn't allow it: tools.ietf.org/html/rfc7159#section-2

    – ForceBru
    Nov 15 '18 at 21:25











  • @FranzKafka, they're strings for Python, but not the JSON parser.

    – ForceBru
    Nov 15 '18 at 21:26











  • 123.6 is a valid number literal, "foo.com" is a valid string literal, but foo.com isn't valid json

    – mata
    Nov 15 '18 at 21:33
















Because 123.6 is valid JSON, but foo.com (without quotes!) is not?

– ForceBru
Nov 15 '18 at 21:20






Because 123.6 is valid JSON, but foo.com (without quotes!) is not?

– ForceBru
Nov 15 '18 at 21:20














@ForceBru Both are quoted, therefore both should be strings.

– Franz Kafka
Nov 15 '18 at 21:23





@ForceBru Both are quoted, therefore both should be strings.

– Franz Kafka
Nov 15 '18 at 21:23













@DeepSpace, because the grammar doesn't allow it: tools.ietf.org/html/rfc7159#section-2

– ForceBru
Nov 15 '18 at 21:25





@DeepSpace, because the grammar doesn't allow it: tools.ietf.org/html/rfc7159#section-2

– ForceBru
Nov 15 '18 at 21:25













@FranzKafka, they're strings for Python, but not the JSON parser.

– ForceBru
Nov 15 '18 at 21:26





@FranzKafka, they're strings for Python, but not the JSON parser.

– ForceBru
Nov 15 '18 at 21:26













123.6 is a valid number literal, "foo.com" is a valid string literal, but foo.com isn't valid json

– mata
Nov 15 '18 at 21:33





123.6 is a valid number literal, "foo.com" is a valid string literal, but foo.com isn't valid json

– mata
Nov 15 '18 at 21:33












2 Answers
2






active

oldest

votes


















2














Quote from RFC7159 that defines JSON:




These are the six structural characters:



 begin-array = ws %x5B ws ; [ left square bracket
begin-object = ws %x7B ws ; left curly bracket
end-array = ws %x5D ws ; ] right square bracket
end-object = ws %x7D ws ; right curly bracket
name-separator = ws %x3A ws ; : colon
value-separator = ws %x2C ws ; , comma


A JSON value MUST be an object, array, number, or string, or one of
the following three literal names:



false null true


<...> No other literal names are allowed.



value = false / null / true / object / array / number / string
object = begin-object [ member *( value-separator member ) ]
end-object
array = begin-array [ value *( value-separator value ) ] end-array
number = [ minus ] int [ frac ] [ exp ]
string = quotation-mark *char quotation-mark



As you can see, the input foo.com does not satisfy any of that:



  • it doesn't start with a begin-object

  • nor does it start with a begin-array

  • nor is it a number

  • nor is it a string, because it does not start with a quotation-mark

However, 123.6 is a number:



123 . 6 == int frac


where, as per the RFC:



frac = decimal-point 1*DIGIT
int = zero / ( digit1-9 *DIGIT )


Thus, 123 . 6 is a value, so it's fine.






share|improve this answer






























    1














    json.loads expects a string. When the JSON parser handles this string all is left is foo.com which is an invalid token. It's an edge case of this example:



    json.loads('a: 1')


    You would not expect this to work, wouldn't you? a is an invalid token.



    For this to work we will need another set of quotes:



    print(json.loads('"foo.com"'))
    # foo.com





    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328019%2fwhy-does-python-3-json-loadsfoo-com-not-work-yet-json-loads123-6-does%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Quote from RFC7159 that defines JSON:




      These are the six structural characters:



       begin-array = ws %x5B ws ; [ left square bracket
      begin-object = ws %x7B ws ; left curly bracket
      end-array = ws %x5D ws ; ] right square bracket
      end-object = ws %x7D ws ; right curly bracket
      name-separator = ws %x3A ws ; : colon
      value-separator = ws %x2C ws ; , comma


      A JSON value MUST be an object, array, number, or string, or one of
      the following three literal names:



      false null true


      <...> No other literal names are allowed.



      value = false / null / true / object / array / number / string
      object = begin-object [ member *( value-separator member ) ]
      end-object
      array = begin-array [ value *( value-separator value ) ] end-array
      number = [ minus ] int [ frac ] [ exp ]
      string = quotation-mark *char quotation-mark



      As you can see, the input foo.com does not satisfy any of that:



      • it doesn't start with a begin-object

      • nor does it start with a begin-array

      • nor is it a number

      • nor is it a string, because it does not start with a quotation-mark

      However, 123.6 is a number:



      123 . 6 == int frac


      where, as per the RFC:



      frac = decimal-point 1*DIGIT
      int = zero / ( digit1-9 *DIGIT )


      Thus, 123 . 6 is a value, so it's fine.






      share|improve this answer



























        2














        Quote from RFC7159 that defines JSON:




        These are the six structural characters:



         begin-array = ws %x5B ws ; [ left square bracket
        begin-object = ws %x7B ws ; left curly bracket
        end-array = ws %x5D ws ; ] right square bracket
        end-object = ws %x7D ws ; right curly bracket
        name-separator = ws %x3A ws ; : colon
        value-separator = ws %x2C ws ; , comma


        A JSON value MUST be an object, array, number, or string, or one of
        the following three literal names:



        false null true


        <...> No other literal names are allowed.



        value = false / null / true / object / array / number / string
        object = begin-object [ member *( value-separator member ) ]
        end-object
        array = begin-array [ value *( value-separator value ) ] end-array
        number = [ minus ] int [ frac ] [ exp ]
        string = quotation-mark *char quotation-mark



        As you can see, the input foo.com does not satisfy any of that:



        • it doesn't start with a begin-object

        • nor does it start with a begin-array

        • nor is it a number

        • nor is it a string, because it does not start with a quotation-mark

        However, 123.6 is a number:



        123 . 6 == int frac


        where, as per the RFC:



        frac = decimal-point 1*DIGIT
        int = zero / ( digit1-9 *DIGIT )


        Thus, 123 . 6 is a value, so it's fine.






        share|improve this answer

























          2












          2








          2







          Quote from RFC7159 that defines JSON:




          These are the six structural characters:



           begin-array = ws %x5B ws ; [ left square bracket
          begin-object = ws %x7B ws ; left curly bracket
          end-array = ws %x5D ws ; ] right square bracket
          end-object = ws %x7D ws ; right curly bracket
          name-separator = ws %x3A ws ; : colon
          value-separator = ws %x2C ws ; , comma


          A JSON value MUST be an object, array, number, or string, or one of
          the following three literal names:



          false null true


          <...> No other literal names are allowed.



          value = false / null / true / object / array / number / string
          object = begin-object [ member *( value-separator member ) ]
          end-object
          array = begin-array [ value *( value-separator value ) ] end-array
          number = [ minus ] int [ frac ] [ exp ]
          string = quotation-mark *char quotation-mark



          As you can see, the input foo.com does not satisfy any of that:



          • it doesn't start with a begin-object

          • nor does it start with a begin-array

          • nor is it a number

          • nor is it a string, because it does not start with a quotation-mark

          However, 123.6 is a number:



          123 . 6 == int frac


          where, as per the RFC:



          frac = decimal-point 1*DIGIT
          int = zero / ( digit1-9 *DIGIT )


          Thus, 123 . 6 is a value, so it's fine.






          share|improve this answer













          Quote from RFC7159 that defines JSON:




          These are the six structural characters:



           begin-array = ws %x5B ws ; [ left square bracket
          begin-object = ws %x7B ws ; left curly bracket
          end-array = ws %x5D ws ; ] right square bracket
          end-object = ws %x7D ws ; right curly bracket
          name-separator = ws %x3A ws ; : colon
          value-separator = ws %x2C ws ; , comma


          A JSON value MUST be an object, array, number, or string, or one of
          the following three literal names:



          false null true


          <...> No other literal names are allowed.



          value = false / null / true / object / array / number / string
          object = begin-object [ member *( value-separator member ) ]
          end-object
          array = begin-array [ value *( value-separator value ) ] end-array
          number = [ minus ] int [ frac ] [ exp ]
          string = quotation-mark *char quotation-mark



          As you can see, the input foo.com does not satisfy any of that:



          • it doesn't start with a begin-object

          • nor does it start with a begin-array

          • nor is it a number

          • nor is it a string, because it does not start with a quotation-mark

          However, 123.6 is a number:



          123 . 6 == int frac


          where, as per the RFC:



          frac = decimal-point 1*DIGIT
          int = zero / ( digit1-9 *DIGIT )


          Thus, 123 . 6 is a value, so it's fine.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 21:33









          ForceBruForceBru

          20.3k83254




          20.3k83254























              1














              json.loads expects a string. When the JSON parser handles this string all is left is foo.com which is an invalid token. It's an edge case of this example:



              json.loads('a: 1')


              You would not expect this to work, wouldn't you? a is an invalid token.



              For this to work we will need another set of quotes:



              print(json.loads('"foo.com"'))
              # foo.com





              share|improve this answer



























                1














                json.loads expects a string. When the JSON parser handles this string all is left is foo.com which is an invalid token. It's an edge case of this example:



                json.loads('a: 1')


                You would not expect this to work, wouldn't you? a is an invalid token.



                For this to work we will need another set of quotes:



                print(json.loads('"foo.com"'))
                # foo.com





                share|improve this answer

























                  1












                  1








                  1







                  json.loads expects a string. When the JSON parser handles this string all is left is foo.com which is an invalid token. It's an edge case of this example:



                  json.loads('a: 1')


                  You would not expect this to work, wouldn't you? a is an invalid token.



                  For this to work we will need another set of quotes:



                  print(json.loads('"foo.com"'))
                  # foo.com





                  share|improve this answer













                  json.loads expects a string. When the JSON parser handles this string all is left is foo.com which is an invalid token. It's an edge case of this example:



                  json.loads('a: 1')


                  You would not expect this to work, wouldn't you? a is an invalid token.



                  For this to work we will need another set of quotes:



                  print(json.loads('"foo.com"'))
                  # foo.com






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 21:31









                  DeepSpaceDeepSpace

                  39.5k44777




                  39.5k44777



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328019%2fwhy-does-python-3-json-loadsfoo-com-not-work-yet-json-loads123-6-does%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      政党