Update and Read Ndb JsonProperty Using Cloud Datastore Client (Python 3)

January 12, 2019

When you view the content of JsonProperty using Datastore Entities Viewer, you shall notice the content is in some giberish encoding (e.g. eyJZUmVzb2***vIjogMX0=). This is actually json string in base64 bytes encoding, though we don’t need to convert to base64 when using Python Client for Google Cloud Datastore.

To write to JsonProperty (Python 3), convert json string to bytes.

import json

data = {'name': 'Desmond'}

# encode in Python 3 will convert string to bytes
json_property = json.dumps(data, separators=(',', ':')).encode()
# b'{"name":"Desmond"}'

To read from JsonProperty (Python 3)

import json

# json.loads accept both uncide str and bytes str
data = json.loads(json_property)

For Python 2.x, the following should work.

import json

data = {'name': 'Desmond'}

# write to json property
json_property = json.dumps(data, separators=(',', ':'))

# read from json property
data = json.loads(json_property)

NOTE: I didn’t test the code for Python 2.x

In Python 3, 'str' is unicode string and b'bytes' is byte string. In Python 2, 'str' is byte string and u'unicde' is unicode string. Refer to Python 2 vs Python 3: Byte, Str and Unicode.

We want to consistenly use byte string (‘str’ in Python 2.x, ‘bytes’ in Python 3.x) for json_property read/write. For Python 3.x, we need to convert str (which is unicode) to bytes (byte string). For Python 2.x, str is already byte string.


This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.