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

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 jsondata = {'name': 'Desmond'}# encode in Python 3 will convert string to bytesjson_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 strdata = json.loads(json_property)

For Python 2.x, the following should work.

import jsondata = {'name': 'Desmond'}# write to json propertyjson_property = json.dumps(data, separators=(',', ':'))# read from json propertydata = 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.


❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.