Python - obtain JSON from URL, control and special characters inside

JD Gamboa

I have been trying to obtain a JSON from a URL with a typical method such as:

import urllib.request, json 
with urllib.request.urlopen("my_url") as url:
    data = json.loads(url.read().decode())

Nevertheless this fails with JSONDecodeError since there is a control character inside some bracket

{..."\tvalue"...}

I DID modify my source data to not include control characters (something I might not always be able to do), and nevertheless python keeps saying the control character is there.

I decided to take my url response into a string, and there replace inner control characters

my_str = url.read()
my_str = my_str.replace('"\\t','"')

But in this way, the special characters throughout the JSON file get replaced in odd ways: {...sábado...} becomes {...sa\cxx\c1bado} or something like that.

How can I sanitize my JSON input from control characters without destroying my special characters?

EDIT:

Sorry, forgot to mention something given the first answer:

I did try adding strict=False, but then my JSON went... well, nuts. The double quotes became single quotes, and some of them would dissapear, so when I would print it, I got something like:

{
    'some_key':'some_value',
    'another_key':'another_value_without_closing_quote,
    a_key_without_opening_quote': 'value'
}

Not a single clue of why was that the case.

JD Gamboa

I ended up solving my predicament by first reading the original JSON into a string. There, I would make the string go through a sanitizing method that would remove escape characters and would replace 'damaged' characters for their original special characters.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

R does not show special characters coming from json source

分類Dev

How to remove special characters except space from a file in python?

分類Dev

Control characters in Python shell

分類Dev

Special characters in identifiers in python 3

分類Dev

Obtain function from string in python

分類Dev

How to calculate value from Json inside Python

分類Dev

BATCH : Change special characters from filenames (é è à...)

分類Dev

How to remove characters and special notations from string

分類Dev

Python Pandas handle special characters in strings

分類Dev

Can we add special characters inside comments of ruby files?

分類Dev

Find word inside special characters or spaces in multiple files

分類Dev

obtain last word from a string in python

分類Dev

Why does the iframe fail to load because of the special characters in the URL?

分類Dev

how to get local characters inside json array

分類Dev

Correctly display special characters in JSON API response using nodeFetch

分類Dev

python - removing all non-numeric characters from a string inside a list

分類Dev

How to remove special characters from a string using Javascript

分類Dev

How to prevent ggvis from showing html encoding for special characters?

分類Dev

Removing numbers and special characters from multi line statement

分類Dev

(Only C) conversion special characters from string char to hexadecimal

分類Dev

Special characters are correctly displayed only when inserted from PHP

分類Dev

Python script failing to correctly encode special Unicode characters

分類Dev

Python regex: removing all special characters and numbers NOT attached to words

分類Dev

How do I obtain results from 'yield' in python?

分類Dev

Obtain and store adjacent values from a .csv file (Python)

分類Dev

Escaping special characters in elasticsearch

分類Dev

sed/ perl with special characters (@)

分類Dev

Lark: parsing special characters

分類Dev

Tokenizing special characters in a string

Related 関連記事

  1. 1

    R does not show special characters coming from json source

  2. 2

    How to remove special characters except space from a file in python?

  3. 3

    Control characters in Python shell

  4. 4

    Special characters in identifiers in python 3

  5. 5

    Obtain function from string in python

  6. 6

    How to calculate value from Json inside Python

  7. 7

    BATCH : Change special characters from filenames (é è à...)

  8. 8

    How to remove characters and special notations from string

  9. 9

    Python Pandas handle special characters in strings

  10. 10

    Can we add special characters inside comments of ruby files?

  11. 11

    Find word inside special characters or spaces in multiple files

  12. 12

    obtain last word from a string in python

  13. 13

    Why does the iframe fail to load because of the special characters in the URL?

  14. 14

    how to get local characters inside json array

  15. 15

    Correctly display special characters in JSON API response using nodeFetch

  16. 16

    python - removing all non-numeric characters from a string inside a list

  17. 17

    How to remove special characters from a string using Javascript

  18. 18

    How to prevent ggvis from showing html encoding for special characters?

  19. 19

    Removing numbers and special characters from multi line statement

  20. 20

    (Only C) conversion special characters from string char to hexadecimal

  21. 21

    Special characters are correctly displayed only when inserted from PHP

  22. 22

    Python script failing to correctly encode special Unicode characters

  23. 23

    Python regex: removing all special characters and numbers NOT attached to words

  24. 24

    How do I obtain results from 'yield' in python?

  25. 25

    Obtain and store adjacent values from a .csv file (Python)

  26. 26

    Escaping special characters in elasticsearch

  27. 27

    sed/ perl with special characters (@)

  28. 28

    Lark: parsing special characters

  29. 29

    Tokenizing special characters in a string

ホットタグ

アーカイブ