4

My raw data are plain text files where each line is a json string. For example:

{"f1": "v1", "f2": [1, 2, 3]}
{"f1": "v2", "f2": [2, 3, 4]}

I read the file into tf.Dataset by

ds = tf.data.TextLineDataset("file_name"))

Now each element of ds is a Tensor with dtype=tf.string.

How do I parse the json strings?

I tried

ds.map(lambda x: json.loads(x)["f2"])

but got

TypeError: the JSON object must be str, bytes or bytearray, not 'Tensor'
Random Certainty
  • 455
  • 6
  • 17
  • Possible duplicate of [how to get string value out of tf.tensor which dtype is string](https://stackoverflow.com/questions/56122670/how-to-get-string-value-out-of-tf-tensor-which-dtype-is-string/56122892#56122892). – giser_yugang Oct 31 '19 at 01:50

0 Answers0