Python Forum
Python .json problem with UTF-8 file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python .json problem with UTF-8 file
#1
When my program tries to deserialize a .json file, it chokes on the UTF-8 designation (EF BB BF) at the beginning of the .json file. 
The error is: "No JSON object could be decoded"

Is there a way to ignore those three characters while reading .json files short of doing a binary read to strip them out?
Reply
#2
Is the json file valid JSON Formatter.
Reply
#3
Yes it is. When you copy the text and put it tn the verifier the special characters do not show up. They are all unprintable and you can't copy them wit a text editor. It is only when you open the json with a hex editor that you can see them. Strip them out and json deserializes the file just fine.
Reply
#4
Are you using python 2.7? Python 3 handles utf-8 naturally but for python 2.7 you have to put #-*- coding: utf-8 -*- under the shebang

#!/usr/bin/env python
# -*- coding: utf-8 -*-
    .....code here.....
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
The file is saved with UTF-8 Byte Order Mark (BOM).
You can try with utf-8-sig
import json
import codecs

json.load(codecs.open('sample.json', 'r', 'utf-8-sig'))
Reply
#6
This one was actually resolved by the person who created the json file. He removed the UTF-8 BOM at the front of the file. However, the solutions above have been noted for the future. Thank you !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 60 Yesterday, 05:16 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 669 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 2,346 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,962 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,170 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,222 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Writing to json file ebolisa 1 970 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Trying to parse only 3 key values from json file cubangt 8 3,338 Jul-16-2022, 02:05 PM
Last Post: deanhystad
  Python Split json into separate json based on node value CzarR 1 5,476 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  Problem with importing Python file in Visual Studio Code DXav 7 4,911 Jun-15-2022, 12:54 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020