Python Forum
Error message in Jupyter Notebook with json
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error message in Jupyter Notebook with json
#3
The problem is that you have bytes b'something' in dictionary data.
JSON format only supports Unicode strings.
As mention bye @volcano63 you overwrite format,but also bytes is a used name bye Python.
>>> format
<built-in function format>
>>> bytes
<class 'bytes'>

>>> # A name that not used give NameError
>>> foo
Traceback (most recent call last):
  File "<string>", line 428, in runcode
  File "<interactive input>", line 1, in <module>
NameError: name 'foo' is not defined 
You should also show what you call function with,now do i see that in error message that it's png image.
Since Base64 encodes bytes to ASCII-only bytes,you can use that codec to decode the data so it can be dumped to JSON.
import base64
import json
import subprocess
 
def output_image(name, file_format, img_bytes):
    image_start = "BEGIN_IMAGE_f9825uweof8jw9fj4r8"
    image_end = "END_IMAGE_0238jfw08fjsiufhw8frs"
    data = {}
    data['name'] = name
    data['format'] = file_format
    encoded = data['bytes'] = base64.encodestring(img_bytes)
    # Fix
    data['bytes'] = encoded.decode('ascii')     
    print(image_start + json.dumps(data) + image_end)
    
output_image('logo.png', 'png', open("logo.png", "rb").read())
Reply


Messages In This Thread
RE: Error message in Jupyter Notebook with json - by snippsat - Jun-17-2018, 01:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  json loads throwing error mpsameer 8 827 Jan-23-2024, 07:04 AM
Last Post: deanhystad
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 1,205 Nov-15-2023, 06:56 PM
Last Post: jst
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 824 Oct-29-2023, 12:40 PM
Last Post: Mark17
  Error message about iid from RandomizedSearchCV Visiting 2 1,081 Aug-17-2023, 07:53 PM
Last Post: Visiting
  json decoding error deneme2 10 4,000 Mar-22-2023, 10:44 PM
Last Post: deanhystad
  Another Error message. the_jl_zone 2 1,022 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  How to programmatically stop a program in Jupyter Notebook? Mark17 11 38,034 Feb-12-2023, 01:41 PM
Last Post: jp21in
Thumbs Up Python 3 Jupyter notebook ternary plot data nicholas 0 997 Jan-21-2023, 05:01 PM
Last Post: nicholas
  python requests library .JSON() error mHosseinDS86 6 3,648 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  Graphs from Jupyter notebook to word Scott 0 933 Nov-28-2022, 06:16 AM
Last Post: Scott

Forum Jump:

User Panel Messages

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