← Back to Blog

5 Techniques for Flattening Deeply Nested JSON

March 16, 2026 3 min read By CodeTidy Team

The Never-Ending Nightmare of Deeply Nested JSON

We've all been there - staring at a seemingly endless JSON object, wondering how to extract the data we need without losing our minds. Deeply nested JSON can be a real challenge, especially when working with large datasets or complex APIs. But fear not, dear developer! Today, we'll explore five techniques for flattening deeply nested JSON, making your life easier and your code cleaner.

Table of Contents

  • Recursive Flatten
  • Dot-Notation Keys
  • Using Lodash
  • jq to the Rescue
  • Pythonic Solutions
  • Key Takeaways
  • FAQ

Recursive Flatten

One of the most straightforward approaches is to recursively flatten the JSON object. This involves iterating over the object's properties and recursively calling the same function on any nested objects. Here's an example in JavaScript:

function recursiveFlatten(obj) {
  const flattened = {};

  for (const key in obj) {
    if (typeof obj[key] === 'object') {
      const nested = recursiveFlatten(obj[key]);
      for (const nestedKey in nested) {
        flattened[`${key}.${nestedKey}`] = nested[nestedKey];
      }
    } else {
      flattened[key] = obj[key];
    }
  }

  return flattened;
}

Let's take a look at an example JSON object:

{
  "name": "John",
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "zip": "12345"
  },
  "interests": [
    {
      "name": "reading",
      "description": "I love reading books"
    },
    {
      "name": "hiking",
      "description": "I love hiking in the mountains"
    }
  ]
}

Using our recursive function, we can flatten this object into:

{
  "name": "John",
  "address.street": "123 Main St",
  "address.city": "Anytown",
  "address.state": "CA",
  "address.zip": "12345",
  "interests.0.name": "reading",
  "interests.0.description": "I love reading books",
  "interests.1.name": "hiking",
  "interests.1.description": "I love hiking in the mountains"
}

Dot-Notation Keys

Another approach is to use dot-notation keys to represent nested objects. This involves replacing nested objects with a single key that represents the path to the value. Here's an example in Python:

import json

def dot_notation_keys(obj):
  result = {}
  for key, value in obj.items():
    if isinstance(value, dict):
      for subkey, subvalue in value.items():
        result[f"{key}.{subkey}"] = subvalue
    else:
      result[key] = value
  return result

Using the same example JSON object, we can transform it into:

{
  "name": "John",
  "address.street": "123 Main St",
  "address.city": "Anytown",
  "address.state": "CA",
  "address.zip": "12345",
  "interests.0.name": "reading",
  "interests.0.description": "I love reading books",
  "interests.1.name": "hiking",
  "interests.1.description": "I love hiking in the mountains"
}

Using Lodash

If you're working with JavaScript, you can use the popular Lodash library to flatten your JSON objects. Specifically, you can use the _.flatten function to achieve this:

const _ = require('lodash');

const json = {
  "name": "John",
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "zip": "12345"
  },
  "interests": [
    {
      "name": "reading",
      "description": "I love reading books"
    },
    {
      "name": "hiking",
      "description": "I love hiking in the mountains"
    }
  ]
};

const flattened = _.flatten(json);
console.log(flattened);

This will output:

[
  "John",
  {
    "street": "123 Main St",
    "city": "Anytown",
    "state": "CA",
    "zip": "12345"
  },
  [
    {
      "name": "reading",
      "description": "I love reading books"
    },
    {
      "name": "hiking",
      "description": "I love hiking in the mountains"
    }
  ]
]

Note that this output is an array, so you may need to further process it to get the desired output.

jq to the Rescue

If you're working with JSON data on the command line, you can use the powerful jq tool to flatten your JSON objects. Here's an example:

jq '. | to_entries[] | .key + "=" + (.value | tostring)' input.json

This will output:

"name=John"
"address.street=123 Main St"
"address.city=Anytown"
"address.state=CA"
"address.zip=12345"
"interests[0].name=reading"
"interests[0].description=I love reading books"
"interests[1].name=hiking"
"interests[1].description=I love hiking in the mountains"

Pythonic Solutions

If you're working with Python, you can use the json library to flatten your JSON objects. Here's an example:

import json

def flatten_json(obj):
  result = {}
  for key, value in obj.items():
    if isinstance(value, dict):
      for subkey, subvalue in value.items():
        result[f"{key}.{subkey}"] = subvalue
    else:
      result[key] = value
  return result

Using the same example JSON object, we can transform it into:

{
  "name": "John",
  "address.street": "123 Main St",
  "address.city": "Anytown",
  "address.state": "CA",
  "address.zip": "12345",
  "interests.0.name": "reading",
  "interests.0.description": "I love reading books",
  "interests.1.name": "hiking",
  "interests.1.description": "I love hiking in the mountains"
}

Key Takeaways

  • Recursive flattening is a straightforward approach to flattening deeply nested JSON objects.
  • Dot-notation keys can be used to represent nested objects in a more compact form.
  • Lodash provides a convenient _.flatten function for JavaScript developers.
  • jq is a powerful tool for working with JSON data on the command line.
  • Python's json library can be used to flatten JSON objects.

FAQ

Q: What is the best approach for flattening deeply nested JSON objects?

A: The best approach depends on your specific use case and requirements. Recursive flattening is a straightforward approach, while dot-notation keys can be more compact. Lodash and jq provide convenient solutions for JavaScript and command-line developers, respectively.

Q: How do I handle arrays in my JSON object?

A: When working with arrays, you can use the same approaches as with objects. However, you may need to use additional techniques, such as using _.flatten in Lodash or jq's to_entries function.

Q: Can I use these techniques with other data formats?

A: While these techniques are specifically designed for JSON data, you can adapt them for other data formats, such as XML or CSV. However, you may need to modify the approaches to accommodate the specific data format.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp