2

I am working with Python and Zookeeper as I am using kazoo library in Python. This question is not about Zookeeper or kazoo library. I guess this is mainly related to Python.

Below are my two variables -

new_error_str = "Error occurred on machine %s in datacenter %s on the %s of process %s" % (host_info, local_dc, step, process_name)
new_error_key = "error_%s" % (timestamp_in_ms)

And now I need to use these two variables to make a byte json string and then write the that json string as the data in the Zookeeper node so below is the syntax by which we create a node and write the data as well -

zk.create(date_znode_path, b'{"'+new_error_key+'":"' + new_error_str + '"}', None, True)

Somehow the above line throws an exception as -

TypeError: value must be a byte string

Any thoughts what wrong I am doing here? I am trying to make a JSON String of above variables.

Here is details about kazoo library

UPDATE:-

If I use this syntax, then it works fine -

b'{"hostname":"' + get_hostname() + '"}', None, True)

I can see data like this -

{"hostname":"machineA"}

1 Answers1

2

You should use json module:

import json
zk.create(date_znode_path, json.dumps({new_error_key : new_error_str}, ensure_ascii=True), None, True)

Manually constructing json is tricky, what if input contains quotes? It is always better to use tools that can escape everything and are well tested against all edge cases.

m.wasowski
  • 6,329
  • 1
  • 23
  • 30
  • 1
    The `bytes` constructor raises an error for me if I pass it a string. You can pass it a second `encoding` argument, but using `str.encode` may be clearer. – Blckknght Mar 28 '14 at 06:33
  • I am running Python version 2.7.3 and just updated the question with an example –  Mar 28 '14 at 06:38
  • I adjusted my answer. Constructing manually json is tricky when encodings come to play, it is best to just delegate to tools that were made for this, right? ;) – m.wasowski Mar 28 '14 at 07:08
  • What is the purpose of using `ensure_ascii=True` here? –  Mar 28 '14 at 07:32
  • @SSH even more conservative than byte, escapes everything outside 128 range. Probably overkill, but won't hurt for sure. – m.wasowski Mar 28 '14 at 07:44
  • sure.. One last question, after storing this string like this. I will be extracting this string back by using UTF-8 format so this should fine right? meaning whatever I am storing it like this, it should come fine when I will read it back using UTF-8 format? –  Mar 31 '14 at 04:41
  • well... I would keep keys in ascii range, but that's like no-brainer for errors, isn't it? Everything else depends on how rest of framework is written, it _should_ work. For sure it won't hard to write a few test cases, but again - it is a no brainer, everything involving different encodings should be covered with tests. – m.wasowski Mar 31 '14 at 06:33