How to convert the json data to tensor?

I have a series json data like this:

[
	{
		"data": [
			{"p": 12.1, "q": 12.35, "g": 3.2},
			{"p": 12.2, "q": 12.36, "g": 3.3},
			{"p": 12.3, "q": 12.37, "g": 3.4}
		],
		"target": 0.001
	},
	{
		"data": [
			{"p": 13.1, "q": 13.35, "g": 4.2},
			{"p": 13.2, "q": 13.36, "g": 4.3},
			{"p": 13.3, "q": 13.37, "g": 4.4}
		],
		"target": 0.002
	},
	{
		"data": [
			{"p": 14.1, "q": 14.35, "g": 5.2},
			{"p": 14.2, "q": 14.36, "g": 5.3},
			{"p": 14.3, "q": 14.37, "g": 5.4}
		],
		"target": 0.003
	}
]

I am trying to read the data by Pandas and convert it to tensor like this

import pandas as pd
import tensorflow as tf

js = pd.read_json('training.txt')
ds = tf.data.Dataset.from_tensor_slices((js.datas.values, js.target.values))

But it returns an error:
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).

So how to convert my json data to tensor?
Thank you!

Why convert it with pandas first? why not json.load?

The data field is currently a list of dictionaries. TensorFlow tries to convert lists to tensors, but it can’t put a dict n a tensor. Flip this to be a dict of str:list and this will work with json.load, IDK about pandas.

1 Like

Thank you to help me! :grinning:

1 Like