How to Fix json.loads Unexpected UTF-8 BOM Error in Python

文章推薦指數: 80 %
投票人數:10人

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.



請為這篇文章評分?