Bottom Page

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?
Quote
#2
Is the json file valid JSON Formatter.
Quote
#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.
Quote
#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
Quote
#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'))
Quote
#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 !
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how to read json file jk91 22 541 Yesterday, 04:50 PM
Last Post: snippsat
  Using a value from one json file to another. pyseeker 0 99 Dec-30-2019, 05:32 AM
Last Post: pyseeker
  problem running python file using cmd panzers 2 186 Dec-19-2019, 04:04 PM
Last Post: panzers
  json problem enigma619 9 321 Dec-19-2019, 08:29 AM
Last Post: enigma619
  Python Parameter inside Json file treated as String dhiliptcs 0 233 Dec-10-2019, 07:28 PM
Last Post: dhiliptcs
  Output to a json file problem Netcode 3 247 Nov-22-2019, 01:44 AM
Last Post: Skaperen
  from Json Time Serie file to python dictionnary Reims 1 239 Sep-11-2019, 08:17 AM
Last Post: DeaD_EyE
  convert a json file to a python dictionnary of array Reims 2 256 Sep-10-2019, 01:08 PM
Last Post: Reims
  Load JSON file data into mongodb using pymongo klllmmm 1 2,850 Jun-28-2019, 12:47 AM
Last Post: klllmmm
  Python problem reading file Lafayette 0 261 Jun-13-2019, 09:04 PM
Last Post: Lafayette

Forum Jump:


Users browsing this thread: 1 Guest(s)