Categories

From Pairs

Convert array of key-value pairs to object using lodash _.fromPairs

Convert an array of key-value pairs into a single object.

Features:

  • Accepts arrays of [key, value] pairs
  • Creates an object from the pairs
  • Handles various data types for values
  • Supports string, number, and symbol keys
  • Uses lodash _.fromPairs for reliable conversion

Examples:

  • Input: [["a", 1], ["b", 2]] → Output: {a: 1, b: 2}
  • Input: [["name", "John"], ["age", 30]] → Output: {name: "John", age: 30}
  • Input: [[1, "one"], [2, "two"]] → Output: {1: "one", 2: "two"}

Use Cases:

  • Object Construction: Build objects from pair arrays
  • Data Transformation: Convert tabular data to object format
  • Configuration Building: Create config objects from key-value lists
  • Data Import: Convert imported pair arrays to objects

Enter a valid JSON array of [key, value] pairs

Key Facts

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

Overview

The From Pairs tool converts an array of key-value pairs into a JavaScript object using the lodash _.fromPairs function. It provides a reliable way to transform structured data, such as lists or tabular inputs, into an object format for easy access and manipulation.

When to Use

  • When you have data in a tabular or list format and need to convert it to an object for programming use.
  • When building configuration objects from key-value pairs, such as settings or parameters.
  • When importing data from sources that output arrays of pairs, like APIs or CSV files, and require an object structure.

How It Works

  • Input a valid JSON array of [key, value] pairs into the tool's text area.
  • The tool processes the array using lodash's _.fromPairs method to ensure accurate conversion.
  • It outputs a single JavaScript object where each pair becomes a property with the key as the name and value as the data.
  • The conversion supports various data types for keys, including strings, numbers, and symbols, and handles diverse value types.

Use Cases

Converting CSV or spreadsheet data exported as key-value pairs into a JavaScript object for application logic.
Creating configuration objects from environment variables or settings lists in development workflows.
Transforming API responses that return arrays of pairs into usable objects for data processing or display.

Examples

1. API Data Conversion

Web Developer
Background
A developer receives user data from an API endpoint that returns an array of key-value pairs, such as [["name", "Alice"], ["age", 30], ["email", "[email protected]"]].
Problem
The data needs to be converted into an object for easy property access in the frontend code.
How to Use
Paste the array into the 'Key-Value Pairs' input field and submit to get the object.
Outcome
The output is a clean object: {name: "Alice", age: 30, email: "[email protected]"}, ready for use in the application.

2. Configuration Setup

Background
A system administrator has a list of configuration parameters in key-value pair format from a deployment script.
Problem
Manually creating the configuration object is time-consuming and prone to syntax errors.
How to Use
Input the pairs as a JSON array, such as [["port", 8080], ["debug", true], ["timeout", 5000]], into the tool.
Outcome
A correctly formatted configuration object is generated: {port: 8080, debug: true, timeout: 5000}, simplifying the setup process.

Try with Samples

json

Related Hubs

FAQ

What input format does the tool accept?

It accepts a valid JSON array of [key, value] pairs, such as [["a", 1], ["b", 2]].

Can it handle non-string keys?

Yes, it supports string, number, and symbol keys as per lodash _.fromPairs functionality.

What happens if the input is invalid?

The tool will return an error or empty output if the input is not a valid JSON array of pairs.

Is there a limit to the number of pairs?

No specific limit is enforced, but performance may vary with very large arrays based on system resources.

Does it support nested objects or arrays as values?

Yes, values can be any data type, including nested objects or arrays, as long as the input is valid JSON.

API Documentation

Request Endpoint

POST /en/api/tools/from-pairs

Request Parameters

Parameter Name Type Required Description
pairs textarea Yes Enter a valid JSON array of [key, value] pairs

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-from-pairs": {
      "name": "from-pairs",
      "description": "Convert array of key-value pairs to object using lodash _.fromPairs",
      "baseUrl": "https://elysiatools.com/mcp/sse?toolId=from-pairs",
      "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]