How to Fix json.loads Unexpected UTF-8 BOM Error in Python
文章推薦指數: 80 %
Solution 1 Decode content using utf-8-sig. In this solution, we can use decode() method on the return value of the string.encode() method. This ... Contact AboutUs WriteforUs Subscribe HowtoFixjson.loadsUnexpectedUTF-8BOMErrorinPython bySpeedySenseEditorial|LastUpdated:October26,2021 InPython,Youwillgetanerrorwhileretrievingthedatafromany3rdpartyAPIrequest.Infact,whenresponsecontentconvertstoJSONformatusingjson.loadsmethod,itthrowsanjson.decoder.JSONDecodeError:UnexpectedUTF-8BOMerror.Inthisarticlewearegoingtoseehowtofixjson.loads()UnexpectedUTF-8BOMerrorinPython. HowtoFixjson.loadsUnexpectedUTF-8BOMerrorinPython.WehaveseensolutionstofixUnexpectedUTF-8BOMerrorswhenusingjson.loadsinPython. Theerrorwasoccurbythejson.loads(r.text),sowhentextcontentconverttoJSONformatwehavegettingfollowingerror: json.decoder.JSONDecodeError:UnexpectedUTF-8BOM(decodeusingutf-8-sig):line1column1(char0) TheresponsecontentcomingfromtheAPI,but\ufeffUnicodecharactercomingatthebeginning.ThisUnicodecharactervalue\ufeff(or\xef\xbb\xbfinbinary)isabyteordermark(BOM)character. Python:Fixjson.loadsUnexpectedjson.decoderUTF-8BOMError Followingare4differentsolutions.Basicallyallfollowingsolutionswehavetodecodethedatausingutf-8-signencoding.Thiswaywecanfixtheerror. Solution1Decodecontentusingutf-8-sig Inthissolution,wecanusedecode()methodonthereturnvalueofthestring.encode()method.Thisisthemostefficientsolutiontofixthiserror. decoded_data=r.text.encode().decode('utf-8-sig') data=json.loads(decoded_data) Solution2Decoderesponsecontent Thissolutionisastraightforwardmethodtofixthisissue.Wehaveusedthedecode()methodonr.content. decoded_data=r.content.decode('utf-8-sig') data=json.loads(decoded_data) Solution3Encoderequests.responseobject Inthissolution,youcanusetheencodingpropertyontheresponseobject.Thiswaywecanskipthepreviousexamplesshowingthecallingofencode()anddecode()methods. r.encoding='utf-8-sig' data=json.loads(r.text) Solution4UsePythoncodecsmodule YoucanusethePythoncodecsmodule.Wecanusethecodecs.decode()methodtodecodethedatausingutf-8-sigencoding.Thecodecs.decode()methodacceptsabytesobject.Thus,youhavetoconvertthestringintoabytesobjectusingencode()method. decoded_data=codecs.decode(r.text.encode(),'utf-8-sig') data=json.loads(decoded_data) BottomLine Allinall,ifthejson.loads()methodthrowsanunexpectedUTF-8BOMerror.ItmeansBOMvaluesareexistingintheresponsedata.Inthisarticlewehaveseen4differentsolutionstoridoutthisjson.loadsUnexpectedUTF-8BOMerrorinPython. Anothermostcommonerror:objectarrayscannotbeloadedwhenallow_pickle=false.Youcancheck fixexception tofindanappropriatesolution. Wehopeyouhavefoundthisarticlehelpful.Letusknowyourquestionsorfeedbackifanythroughthecommentsectioninbelow.Youcansubscribeournewsletterandgetnotifiedwhenwepublishnewarticles.Moreover,youcanexplorehereotherinterestingarticles. ShareTweetSharePinShare1SharesIfyoulikeourarticle,pleaseconsiderbuyingacoffeeforus.Thanksforyoursupport!Buymeacoffee!TagsJSONPythonRequests ShareTweetSharePinShare1Shares Postnavigation PreviousArticleWhatHappensWhenYouInstallTooManyShopifyAppsNextArticle6BestAndroidAppsToImproveYourVocabulary JointheDiscussion.Cancel Savemyname,email,andwebsiteinthisbrowserforthenexttimeIcomment. Δ Search LatestPosts WhatistheDarkWeb?HowtoPreventYourDatafromBeingStolen?August30,2022 11BestAngularJSFrameworksforYourNextWebAppDevelopmentAugust10,2022 ShopifyMultivendorMarketplaceMay30,2022 HowtoEmbedaVideoinWordPressBlogPosts?[TheUltimateGuide]April3,2022 CategoriesCategoriesSelectCategoryCloudComputingDatabaseE-CommerceExamFreebiesGeneralHTML/CSSJavaScriptjQueryMobileAppsMobileWebsitesNetworkingOdooOnlineToolsPHPProgrammingPythonSEOShopifyStandardSwiftUbuntuUncategorizedWordpress Subscribe Enteryouremailaddresstosubscribetothisblogandreceivenotificationsofnewpostsbyemail.
延伸文章資訊
- 1Python : How to fix Unexpected UTF-8 BOM error when using ...
So the solution is simple. We just need to decode the data using utf-8-sig encoding, which will g...
- 2json.decoder.JSONDecodeError: Unexpected UTF-8 BOM ...
Unexpected UTF-8 BOM (decode using utf-8-sig). 热门推荐.
- 3line 1 column 1 (char 0) ---While Tuning gpt2.finetune ...
問題描述JSONDecodeError: Unexpected UTF‑8 BOM (decode using utf‑8‑sig): line 1 column 1 (char 0) ‑‑‑W...
- 4Unexpected UTF-8 BOM in JSON response - Google Groups
json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (cha...
- 5What is the difference between utf-8 and utf-8-sig? - Stack Overflow