Categories

Invert By

Invert keys grouped by values using lodash _.invertBy

Group keys by their values or by a transformed value.

Highlights:

  • Uses lodash _.invertBy
  • Groups keys into arrays
  • Optional property path iteratee

Example:

  • Object: {"a":"x","b":"x","c":"y"}
  • Result: {"x":["a","b"],"y":["c"]}

Enter a JSON object

Optional property path for value transform

Key Facts

Category
Data Processing
Input Types
textarea, text
Output Type
text
Sample Coverage
4
API Ready
Yes

Overview

The Invert By tool allows you to restructure JSON objects by grouping keys based on their corresponding values. Powered by the lodash _.invertBy method, this utility simplifies data transformation by collecting keys into arrays associated with unique values or transformed property paths.

When to Use

  • When you need to categorize a flat list of items by a specific shared attribute.
  • When you want to identify all keys that share the same value within a large JSON dataset.
  • When you need to group nested object properties based on a specific path for easier data analysis.

How It Works

  • Paste your source JSON object into the input area.
  • Optionally, provide an 'Iteratee Path' if you want to group by a specific property within the values.
  • Click the process button to generate a new object where values become keys and original keys are grouped into arrays.
  • Copy or download the resulting JSON output for use in your application.

Use Cases

Aggregating user IDs by their assigned roles or status codes.
Organizing configuration settings by their environment or type.
Mapping database records to specific categories for faster lookup.

Examples

1. Categorizing User Roles

Background
A developer has a list of users and their roles and needs to generate a list of users per role for a dashboard.
Problem
The current data is structured as {user: role}, but the UI requires {role: [users]}.
How to Use
Input the user-role object and run the tool to invert the mapping.
Example Config
{"Alice":"Admin", "Bob":"Editor", "Charlie":"Admin"}
Outcome
{"Admin":["Alice","Charlie"], "Editor":["Bob"]}

2. Grouping by Nested Property

Background
An API response contains items with nested status information that needs to be grouped.
Problem
Need to group items by the 'status' property located inside a nested object.
How to Use
Input the object and specify 'status' in the Iteratee Path field.
Example Config
{"item1":{"status":"active"}, "item2":{"status":"pending"}, "item3":{"status":"active"}}
Outcome
{"active":["item1","item3"], "pending":["item2"]}

Try with Samples

json

Related Hubs

FAQ

What does the Invert By tool do?

It transforms an object by swapping its keys and values, grouping all original keys that share the same value into an array.

What is the 'Iteratee Path' used for?

It allows you to group keys based on a specific property path within the values, rather than the values themselves.

Is this tool based on a specific library?

Yes, it utilizes the lodash _.invertBy function to ensure consistent and reliable data processing.

What happens if multiple keys have the same value?

The tool will group all those keys into a single array under that value in the resulting object.

Can I process nested objects?

Yes, by using the Iteratee Path, you can target specific nested properties to determine how the keys should be grouped.

API Documentation

Request Endpoint

POST /en/api/tools/invert-by

Request Parameters

Parameter Name Type Required Description
objectInput textarea Yes Enter a JSON object
iterateePath text No Optional property path for value transform

Response Format

{
  "result": "Processed text content",
  "error": "Error message (optional)",
  "message": "Notification message (optional)",
  "metadata": {
    "key": "value"
  }
}
Text: Text

AI MCP Documentation

Add this tool to your MCP server configuration:

{
  "mcpServers": {
    "elysiatools-invert-by": {
      "name": "invert-by",
      "description": "Invert keys grouped by values using lodash _.invertBy",
      "baseUrl": "https://elysiatools.com/mcp/sse?toolId=invert-by",
      "command": "",
      "args": [],
      "env": {},
      "isActive": true,
      "type": "sse"
    }
  }
}

You can chain multiple tools, e.g.: `https://elysiatools.com/mcp/sse?toolId=png-to-webp,jpg-to-webp,gif-to-webp`, max 20 tools.

If you encounter any issues, please contact us at [email protected]