Python not reading valid JSONCalling an external command in PythonWhat are metaclasses in Python?Can comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?Does Python have a ternary conditional operator?What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?How to read a file line-by-line into a list?Does Python have a string 'contains' substring method?

Fencing style for blades that can attack from a distance

How to format long polynomial?

In Japanese, what’s the difference between “Tonari ni” (となりに) and “Tsugi” (つぎ)? When would you use one over the other?

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

How is it possible to have an ability score that is less than 3?

Windows 98 hangs after entering password on fresh install

What is the word for reserving something for yourself before others do?

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?

Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)

Why dont electromagnetic waves interact with each other?

Why do falling prices hurt debtors?

What are these boxed doors outside store fronts in New York?

Why are 150k or 200k jobs considered good when there are 300k+ births a month?

I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine

Which models of the Boeing 737 are still in production?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

How to find program name(s) of an installed package?

Expeditious Retreat

How old can references or sources in a thesis be?

What do you call a Matrix-like slowdown and camera movement effect?

How does one intimidate enemies without having the capacity for violence?

What does it mean to describe someone as a butt steak?

Can I make popcorn with any corn?

Do I have a twin with permutated remainders?



Python not reading valid JSON


Calling an external command in PythonWhat are metaclasses in Python?Can comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?Does Python have a ternary conditional operator?What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?How to read a file line-by-line into a list?Does Python have a string 'contains' substring method?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am scraping some HTML source from a web page to extract data stored in a json format



This is the Code:



url = 'https://finance.yahoo.com/quote/SPY'
result = requests.get(url)

c = result.content
html = BeautifulSoup(c, 'html.parser')
scripts = html.find_all('script')

sl =[]
for s in scripts:

sl.append(s)

s = (sl[-3])
s = s.contents
s = str(s)
s = s[119:-16]

json_data = json.loads(s)


Running the above throws this error:



json.decoder.JSONDecodError: Expecting ',' delimiter: line 1 column 7506 (char7505)


When I take the content of variable s and pass it to a json formatter it's recognized as proper json.



I used the following web site to check the json:
http://jsonprettyprint.com/json-pretty-printer.php



Why is this error coming up when using json.loads() in Python? I am assuming it has something to do with the string not being encoded properly or the presence of escape characters?



How do I solve this?










share|improve this question






















  • could u print your JSON before json.loads. You must be missing some kind of delimiter in there

    – skaul05
    Mar 8 at 3:17












  • The json object is too big for me to post here as text

    – Mustard Tiger
    Mar 8 at 3:28

















1















I am scraping some HTML source from a web page to extract data stored in a json format



This is the Code:



url = 'https://finance.yahoo.com/quote/SPY'
result = requests.get(url)

c = result.content
html = BeautifulSoup(c, 'html.parser')
scripts = html.find_all('script')

sl =[]
for s in scripts:

sl.append(s)

s = (sl[-3])
s = s.contents
s = str(s)
s = s[119:-16]

json_data = json.loads(s)


Running the above throws this error:



json.decoder.JSONDecodError: Expecting ',' delimiter: line 1 column 7506 (char7505)


When I take the content of variable s and pass it to a json formatter it's recognized as proper json.



I used the following web site to check the json:
http://jsonprettyprint.com/json-pretty-printer.php



Why is this error coming up when using json.loads() in Python? I am assuming it has something to do with the string not being encoded properly or the presence of escape characters?



How do I solve this?










share|improve this question






















  • could u print your JSON before json.loads. You must be missing some kind of delimiter in there

    – skaul05
    Mar 8 at 3:17












  • The json object is too big for me to post here as text

    – Mustard Tiger
    Mar 8 at 3:28













1












1








1








I am scraping some HTML source from a web page to extract data stored in a json format



This is the Code:



url = 'https://finance.yahoo.com/quote/SPY'
result = requests.get(url)

c = result.content
html = BeautifulSoup(c, 'html.parser')
scripts = html.find_all('script')

sl =[]
for s in scripts:

sl.append(s)

s = (sl[-3])
s = s.contents
s = str(s)
s = s[119:-16]

json_data = json.loads(s)


Running the above throws this error:



json.decoder.JSONDecodError: Expecting ',' delimiter: line 1 column 7506 (char7505)


When I take the content of variable s and pass it to a json formatter it's recognized as proper json.



I used the following web site to check the json:
http://jsonprettyprint.com/json-pretty-printer.php



Why is this error coming up when using json.loads() in Python? I am assuming it has something to do with the string not being encoded properly or the presence of escape characters?



How do I solve this?










share|improve this question














I am scraping some HTML source from a web page to extract data stored in a json format



This is the Code:



url = 'https://finance.yahoo.com/quote/SPY'
result = requests.get(url)

c = result.content
html = BeautifulSoup(c, 'html.parser')
scripts = html.find_all('script')

sl =[]
for s in scripts:

sl.append(s)

s = (sl[-3])
s = s.contents
s = str(s)
s = s[119:-16]

json_data = json.loads(s)


Running the above throws this error:



json.decoder.JSONDecodError: Expecting ',' delimiter: line 1 column 7506 (char7505)


When I take the content of variable s and pass it to a json formatter it's recognized as proper json.



I used the following web site to check the json:
http://jsonprettyprint.com/json-pretty-printer.php



Why is this error coming up when using json.loads() in Python? I am assuming it has something to do with the string not being encoded properly or the presence of escape characters?



How do I solve this?







python html json string encoding






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 3:12









Mustard TigerMustard Tiger

78321333




78321333












  • could u print your JSON before json.loads. You must be missing some kind of delimiter in there

    – skaul05
    Mar 8 at 3:17












  • The json object is too big for me to post here as text

    – Mustard Tiger
    Mar 8 at 3:28

















  • could u print your JSON before json.loads. You must be missing some kind of delimiter in there

    – skaul05
    Mar 8 at 3:17












  • The json object is too big for me to post here as text

    – Mustard Tiger
    Mar 8 at 3:28
















could u print your JSON before json.loads. You must be missing some kind of delimiter in there

– skaul05
Mar 8 at 3:17






could u print your JSON before json.loads. You must be missing some kind of delimiter in there

– skaul05
Mar 8 at 3:17














The json object is too big for me to post here as text

– Mustard Tiger
Mar 8 at 3:28





The json object is too big for me to post here as text

– Mustard Tiger
Mar 8 at 3:28












4 Answers
4






active

oldest

votes


















1














Your JSON contains certain unexpected tokens like true. Use json.dumps first to resolve it.



print (json.dumps(s,indent =2))
s = json.dumps(s)
json_data = json.loads(s)





share|improve this answer






























    1














    json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 7484 (char 7483)


    Using the failure message, you can print a slice of the string to see where it is failing.



    print(s[7400:7500])
    mailboxes.isPrimary=\"true\" AND ymreq


    As skaul05 stated, it is failing because of the true token in the string.






    share|improve this answer






























      -1














      import requests
      from bs4 import BeautifulSoup
      import json

      url = 'https://finance.yahoo.com/quote/SPY'
      result = requests.get(url)

      c = result.content
      html = BeautifulSoup(c, 'html.parser')
      scripts = html.find_all('script')

      sl =[]
      for s in scripts:

      sl.append(s)

      s = (sl[-3])
      s = s.contents

      a = s[0][111:-12]

      jjjj = json.loads(a)


      there's somethingrong when you deal with the list,you just use str()






      share|improve this answer























      • why? it's just where you wrong? why give me such an evaluation?

        – Tom.chen.kang
        Mar 8 at 3:55











      • can you solve this problem by the method the second one provide? hehe

        – Tom.chen.kang
        Mar 8 at 3:57


















      -1














      If it was a valid JSON formatted text then the parser wouldn't complain. This is how I tested it



      //first I scraped that page
      curl https://finance.yahoo.com/quote/SPY > SPY.json
      //then tried to parse it using json
      a = open("SPY.json")
      b = json.load(a)
      ValueError: No JSON object could be decoded


      You probably need to first parse it in to valid xml first.






      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%2f55056192%2fpython-not-reading-valid-json%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Your JSON contains certain unexpected tokens like true. Use json.dumps first to resolve it.



        print (json.dumps(s,indent =2))
        s = json.dumps(s)
        json_data = json.loads(s)





        share|improve this answer



























          1














          Your JSON contains certain unexpected tokens like true. Use json.dumps first to resolve it.



          print (json.dumps(s,indent =2))
          s = json.dumps(s)
          json_data = json.loads(s)





          share|improve this answer

























            1












            1








            1







            Your JSON contains certain unexpected tokens like true. Use json.dumps first to resolve it.



            print (json.dumps(s,indent =2))
            s = json.dumps(s)
            json_data = json.loads(s)





            share|improve this answer













            Your JSON contains certain unexpected tokens like true. Use json.dumps first to resolve it.



            print (json.dumps(s,indent =2))
            s = json.dumps(s)
            json_data = json.loads(s)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 8 at 3:37









            skaul05skaul05

            1,0831616




            1,0831616























                1














                json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 7484 (char 7483)


                Using the failure message, you can print a slice of the string to see where it is failing.



                print(s[7400:7500])
                mailboxes.isPrimary=\"true\" AND ymreq


                As skaul05 stated, it is failing because of the true token in the string.






                share|improve this answer



























                  1














                  json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 7484 (char 7483)


                  Using the failure message, you can print a slice of the string to see where it is failing.



                  print(s[7400:7500])
                  mailboxes.isPrimary=\"true\" AND ymreq


                  As skaul05 stated, it is failing because of the true token in the string.






                  share|improve this answer

























                    1












                    1








                    1







                    json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 7484 (char 7483)


                    Using the failure message, you can print a slice of the string to see where it is failing.



                    print(s[7400:7500])
                    mailboxes.isPrimary=\"true\" AND ymreq


                    As skaul05 stated, it is failing because of the true token in the string.






                    share|improve this answer













                    json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 7484 (char 7483)


                    Using the failure message, you can print a slice of the string to see where it is failing.



                    print(s[7400:7500])
                    mailboxes.isPrimary=\"true\" AND ymreq


                    As skaul05 stated, it is failing because of the true token in the string.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 8 at 3:53









                    ap288ap288

                    365




                    365





















                        -1














                        import requests
                        from bs4 import BeautifulSoup
                        import json

                        url = 'https://finance.yahoo.com/quote/SPY'
                        result = requests.get(url)

                        c = result.content
                        html = BeautifulSoup(c, 'html.parser')
                        scripts = html.find_all('script')

                        sl =[]
                        for s in scripts:

                        sl.append(s)

                        s = (sl[-3])
                        s = s.contents

                        a = s[0][111:-12]

                        jjjj = json.loads(a)


                        there's somethingrong when you deal with the list,you just use str()






                        share|improve this answer























                        • why? it's just where you wrong? why give me such an evaluation?

                          – Tom.chen.kang
                          Mar 8 at 3:55











                        • can you solve this problem by the method the second one provide? hehe

                          – Tom.chen.kang
                          Mar 8 at 3:57















                        -1














                        import requests
                        from bs4 import BeautifulSoup
                        import json

                        url = 'https://finance.yahoo.com/quote/SPY'
                        result = requests.get(url)

                        c = result.content
                        html = BeautifulSoup(c, 'html.parser')
                        scripts = html.find_all('script')

                        sl =[]
                        for s in scripts:

                        sl.append(s)

                        s = (sl[-3])
                        s = s.contents

                        a = s[0][111:-12]

                        jjjj = json.loads(a)


                        there's somethingrong when you deal with the list,you just use str()






                        share|improve this answer























                        • why? it's just where you wrong? why give me such an evaluation?

                          – Tom.chen.kang
                          Mar 8 at 3:55











                        • can you solve this problem by the method the second one provide? hehe

                          – Tom.chen.kang
                          Mar 8 at 3:57













                        -1












                        -1








                        -1







                        import requests
                        from bs4 import BeautifulSoup
                        import json

                        url = 'https://finance.yahoo.com/quote/SPY'
                        result = requests.get(url)

                        c = result.content
                        html = BeautifulSoup(c, 'html.parser')
                        scripts = html.find_all('script')

                        sl =[]
                        for s in scripts:

                        sl.append(s)

                        s = (sl[-3])
                        s = s.contents

                        a = s[0][111:-12]

                        jjjj = json.loads(a)


                        there's somethingrong when you deal with the list,you just use str()






                        share|improve this answer













                        import requests
                        from bs4 import BeautifulSoup
                        import json

                        url = 'https://finance.yahoo.com/quote/SPY'
                        result = requests.get(url)

                        c = result.content
                        html = BeautifulSoup(c, 'html.parser')
                        scripts = html.find_all('script')

                        sl =[]
                        for s in scripts:

                        sl.append(s)

                        s = (sl[-3])
                        s = s.contents

                        a = s[0][111:-12]

                        jjjj = json.loads(a)


                        there's somethingrong when you deal with the list,you just use str()







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Mar 8 at 3:41









                        Tom.chen.kangTom.chen.kang

                        265




                        265












                        • why? it's just where you wrong? why give me such an evaluation?

                          – Tom.chen.kang
                          Mar 8 at 3:55











                        • can you solve this problem by the method the second one provide? hehe

                          – Tom.chen.kang
                          Mar 8 at 3:57

















                        • why? it's just where you wrong? why give me such an evaluation?

                          – Tom.chen.kang
                          Mar 8 at 3:55











                        • can you solve this problem by the method the second one provide? hehe

                          – Tom.chen.kang
                          Mar 8 at 3:57
















                        why? it's just where you wrong? why give me such an evaluation?

                        – Tom.chen.kang
                        Mar 8 at 3:55





                        why? it's just where you wrong? why give me such an evaluation?

                        – Tom.chen.kang
                        Mar 8 at 3:55













                        can you solve this problem by the method the second one provide? hehe

                        – Tom.chen.kang
                        Mar 8 at 3:57





                        can you solve this problem by the method the second one provide? hehe

                        – Tom.chen.kang
                        Mar 8 at 3:57











                        -1














                        If it was a valid JSON formatted text then the parser wouldn't complain. This is how I tested it



                        //first I scraped that page
                        curl https://finance.yahoo.com/quote/SPY > SPY.json
                        //then tried to parse it using json
                        a = open("SPY.json")
                        b = json.load(a)
                        ValueError: No JSON object could be decoded


                        You probably need to first parse it in to valid xml first.






                        share|improve this answer



























                          -1














                          If it was a valid JSON formatted text then the parser wouldn't complain. This is how I tested it



                          //first I scraped that page
                          curl https://finance.yahoo.com/quote/SPY > SPY.json
                          //then tried to parse it using json
                          a = open("SPY.json")
                          b = json.load(a)
                          ValueError: No JSON object could be decoded


                          You probably need to first parse it in to valid xml first.






                          share|improve this answer

























                            -1












                            -1








                            -1







                            If it was a valid JSON formatted text then the parser wouldn't complain. This is how I tested it



                            //first I scraped that page
                            curl https://finance.yahoo.com/quote/SPY > SPY.json
                            //then tried to parse it using json
                            a = open("SPY.json")
                            b = json.load(a)
                            ValueError: No JSON object could be decoded


                            You probably need to first parse it in to valid xml first.






                            share|improve this answer













                            If it was a valid JSON formatted text then the parser wouldn't complain. This is how I tested it



                            //first I scraped that page
                            curl https://finance.yahoo.com/quote/SPY > SPY.json
                            //then tried to parse it using json
                            a = open("SPY.json")
                            b = json.load(a)
                            ValueError: No JSON object could be decoded


                            You probably need to first parse it in to valid xml first.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 8 at 3:49









                            Lawrence KhanLawrence Khan

                            216




                            216



























                                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%2f55056192%2fpython-not-reading-valid-json%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

                                Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                                Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                                Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved