How to read json files in Tensorflow?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
TensorFlow does not treat arbitrary JSON as a first-class training format the way it treats TFRecord or image folders. In practice, you usually read JSON with normal Python tools, convert the fields into tensors, and then build a tf.data.Dataset. The best approach depends on whether the file is a single JSON array or newline-delimited JSON where each line is its own object.
Read a Regular JSON File With Python First
If the file is a normal JSON document, the simplest path is to load it with Python’s json module and then convert the result into tensors.
Example file named samples.json:
Read it like this:
This is often the clearest choice for small and medium datasets.
Convert JSON Fields Into the Shapes Your Model Expects
JSON values are flexible, but TensorFlow likes explicit shapes and dtypes. If a model expects float32 features and integer labels, convert them deliberately.
Being explicit here prevents a lot of downstream shape and dtype confusion.
Use TextLineDataset for Newline-Delimited JSON
Large pipelines often store data as one JSON object per line, sometimes called JSONL or NDJSON. That format works better with streaming input because you can read line by line.
Example file:
A practical TensorFlow pipeline looks like this:
This approach keeps memory use lower for large line-based datasets because the whole file does not need to be loaded into a Python list first.
Choose the Right Pattern for the File Shape
A good rule is:
- use
json.loadfor a regular JSON document - use
TextLineDatasetfor one-object-per-line files - use TFRecord instead of JSON if the dataset is very large and performance matters
TensorFlow can absolutely work with JSON, but JSON is often more of an interchange format than a high-performance training format.
Handle Nested or Irregular Data Carefully
If the JSON contains nested objects, variable-length lists, or missing fields, convert it into a consistent tensor-friendly structure before training.
For example, if every record has a variable number of token IDs, you may need padding or ragged tensors instead of a plain dense tensor. If fields are optional, fill defaults during parsing so later dataset steps see a stable structure.
The main goal is to transform flexible JSON into a predictable tensor schema.
Build a Real Input Pipeline
Once the data is parsed, you can use normal tf.data optimizations:
Those operations happen after parsing. The cleaner your conversion step is, the easier the rest of the input pipeline becomes.
Common Pitfalls
The most common mistake is expecting TensorFlow to parse arbitrary JSON automatically without a conversion step. Another is forgetting to set shapes after tf.py_function, which leaves TensorFlow with too little static shape information. Developers also mix numeric and string types in JSON and then run into dtype errors later. Loading a huge JSON array into memory can be inefficient, so line-delimited JSON plus TextLineDataset is often better for large files. If performance becomes critical, JSON may simply be the wrong storage format for the training pipeline.
Summary
- TensorFlow usually reads JSON by combining Python parsing with
tf.data. - Use
json.loadfor regular JSON documents andTextLineDatasetfor JSONL files. - Convert fields into explicit TensorFlow dtypes and shapes early.
- Call
set_shapeaftertf.py_functionwhen needed. - Use padding or ragged tensors for variable-length data.
- Consider TFRecord if JSON parsing becomes a training bottleneck.

