python


Python: jsonloads gets error: ValueError: Invalid control character


I have the json string below and trying to parse it like:
testjson = '{"FarRulDsc":"取消手数料| <br/>• 出発前2000円(JPY)\n"}'
json.loads(testjson)
It gets error:
Traceback (most recent call last):
File "./test_failed_pnr_status.py", line 98, in <module>
json.loads(testjson)
File "/usr/lib64/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.6/json/decoder.py", line 336, in raw_decode
obj, end = self._scanner.iterscan(s, **kw).next()
File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "/usr/lib64/python2.6/json/decoder.py", line 183, in JSONObject
value, end = iterscan(s, idx=end, context=context).next()
File "/usr/lib64/python2.6/json/scanner.py", line 55, in iterscan
rval, next_pos = action(m, context)
File "/usr/lib64/python2.6/json/decoder.py", line 155, in JSONString
return scanstring(match.string, match.end(), encoding, strict)
ValueError: Invalid control character at: line 1 column 61 (char 61)
If I skip '\', than it is working:
testjson = testjson.replace('\\','')
or
testjson = testjson.replace('\n','')
What is the actual point of problem?
By default it uses strict=True.
Here is the extract from documentation
If strict is false (True is the default), then control characters will
be allowed inside strings. Control characters in this context are
those with character codes in the 0–31 range, including '\t' (tab),
'\n', '\r' and '\0'.
you can try json.loads(json_str, strict=False)

Related Links

Find nth smallest element in numpy array [duplicate]
How to extract array from the first element of triples in 2d array of triples using numpy
Simulate user clicking in QSystemTrayIcon
Parsing GeoRSS feed with python scripting
User interface optimization when using pack
Selenium doesn't return after starting Chrome but returns after starting Firefox?
Python JSON decoding error TypeError: can't use a string pattern on a bytes-like object
Equivalent of \b word boundary in str.contains?
How to store django objects as session variables ( object is not JSON serializable)?
SQLAlchemy (w/ Postgres) - How to limit full-text search to one column of many indexed?
Split on separators not giving exact output
Python average input not working
Python List Trouble… 2 Lists
Pygame - customisable controls
python functions and subfunctions using default parameters
How to install either pybluez or LightBlue on OSX 10.9 (Mavericks‎)

Categories

HOME
plsql
ibm-watson-cognitive
checkbox
nuxeo
survival-analysis
csvhelper
rfc
google-spreadsheet-api
currency
goutte
pca
ipfs
google-classroom
rfid
gatsby
uiscrollview
tee
qt-installer
session-timeout
chocolatey
thingsboard
avplayeritem
bootstrap-typeahead
data-cleansing
vcard
data-conversion
multichoiceitems
positioning
rowcount
modelandview
recurrence-relation
superpowered
viewstate
pox
color-scheme
liquid-xml
wcf-security
mediawiki-extensions
paho
asmx
encapsulation
openweathermap
azure-availability-set
proof
drawingarea
amazon-machine-learning
tofixed
qos
rxvt
garrys-mod
cfeclipse
hspec
uistackview
picasa
audioqueue
easing
tuxedo
listadapter
distribute
handlebars.java
intellitest
affix
sourcegear-vault
digits
twitter-rest-api
web-component-tester
code-first-migrations
sysfs
multipleselection
google-earth-plugin
pisa
temp-tables
hidden-field
x-ua-compatible
grunt-express
formvalidation-plugin
suffix-array
funq
spdy
level
alertifyjs
android-looper
httpcontext
magicalrecord-2.2
va-list
gridworld
visual-studio-2003
backbone-relational
apache2.2
vertical-scrolling
mvccontrib
datarepeater
cosm
plone-funnelweb
driving-directions
datamember
zend-form-element
symbol-server
delegatecommand
trampolines
google-instant
grooveshark
multibyte-functions
standardized
hardware-infrastructure

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App