I have a JSON dataset, for example:

{"candidate00001": "graceful ones."
 "candidate00002":"One more,Marvelous said, sounding royally bored from his seat."
 "candidate00003":"She tired,Joe said, though not unkindly."

in which, candidate00001 is my first class, candidate00002 is the second class and so on.

I am new to python, so i want to implement a character level classification by Keras using this tutorial.

my problem is that I don't now how to convert my JSON file as x_train and y_train.


2 Answers 2


As mentioned by @Frankstr you want a tutorial on "character-level classification", not handwritten digit recognition as the one you have linked.

Character-level classification is typically done with an RNN or a 1D CNN. See this implementation of Character-level Convolutional Networks for Text Classification for example.

In order to convert your json data to x_train and y_train which can be fed to Keras, you can read it into a dictionary, extract keys and values separately, and binarize your class labels:

import json
from sklearn.preprocessing import LabelBinarizer

with open('data.json', 'r') as f:
    train = json.load(f)

y_train = list(train.values())

lb = LabelBinarizer()
x_train = lb.fit_transform(list(train.keys()))

Make sure your json is properly formed. The example you gave is missing commas after each value. It should be:

{"candidate00001": "graceful ones.",
 "candidate00002":"One more,Marvelous said, sounding royally bored from his seat.",
 "candidate00003":"She tired,Joe said, though not unkindly."

Looking into the tutorial you refer to, it seems to be made for character recognition of handwritten letters and numbers, taken from the MNIST database. Character classification means in this tutorial: Take an image and classify it as a represenation of a certain Unicode or Asci defined character.

The candidates you want to process are strings, well defined characters that need no recognition at all. So the tutorial does not match your interest.

Can you define your interest, what do you want to get out of these candidates, which would be a good classification for these three samples? If you have that, I would suggest to raise it as a new question.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.