Categories

Defaults Object

Apply default values to an object using lodash _.defaults

Assign default values for undefined properties on the target object.

Highlights:

  • Uses lodash _.defaults
  • Only fills in undefined properties
  • Later sources are applied first

Example:

  • Target: {"a":1}
  • Source 1: {"a":2,"b":2}
  • Result: {"a":1,"b":2}

Enter the target JSON object

First source object

Optional source object

Optional source object

Key Facts

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

Overview

The Defaults Object tool allows you to efficiently populate missing properties in a JSON object by merging it with one or more source objects using the lodash _.defaults method.

When to Use

  • When you need to ensure an object has required keys without overwriting existing data.
  • When merging configuration objects where user-defined settings should take precedence over defaults.
  • When cleaning up incomplete data structures by filling in missing fields with predefined values.

How It Works

  • Input your primary target JSON object into the target field.
  • Provide one or more source objects containing the default values you wish to apply.
  • The tool processes the objects, assigning values from sources only to properties that are currently undefined in the target.
  • The resulting JSON object is returned with all missing properties filled.

Use Cases

Applying default configuration settings to a user's partial preferences object.
Filling in missing API response fields with fallback values to prevent runtime errors.
Standardizing data objects by ensuring all expected keys exist with default values.

Examples

1. Applying App Configuration Defaults

Frontend Developer
Background
An application receives a partial user settings object from local storage.
Problem
The app needs to ensure 'theme' and 'notifications' keys exist, but must not overwrite the user's existing 'theme' choice.
How to Use
Paste the user's object into 'Target Object' and the default settings into 'Source 1'.
Example Config
Target: {"theme": "dark"} | Source 1: {"theme": "light", "notifications": true}
Outcome
The result is {"theme": "dark", "notifications": true}, preserving the user's theme while adding the missing notification setting.

2. Standardizing API Data

Backend Engineer
Background
A legacy database returns objects missing several new fields required by the frontend.
Problem
The frontend crashes when accessing undefined properties in the data objects.
How to Use
Use the tool to merge the database object with a source object containing the new default values.
Example Config
Target: {"id": 101} | Source 1: {"status": "active", "retryCount": 0}
Outcome
The resulting object contains the original ID plus the new status and retryCount fields, ensuring compatibility.

Try with Samples

json

Related Hubs

FAQ

Does this tool overwrite existing values?

No, it only assigns values to properties that are undefined in the target object.

What happens if multiple sources have the same key?

The tool applies sources in order; earlier sources take precedence over later ones for the same undefined property.

Which library does this tool use?

It utilizes the lodash _.defaults function for reliable object merging.

Can I use this for nested objects?

This tool performs a shallow merge; it assigns values to top-level undefined properties.

Is the input format strictly JSON?

Yes, the tool expects valid JSON objects in the input fields.

API Documentation

Request Endpoint

POST /en/api/tools/defaults-object

Request Parameters

Parameter Name Type Required Description
targetObject textarea Yes Enter the target JSON object
source1 textarea Yes First source object
source2 textarea No Optional source object
source3 textarea No Optional source object

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-defaults-object": {
      "name": "defaults-object",
      "description": "Apply default values to an object using lodash _.defaults",
      "baseUrl": "https://elysiatools.com/mcp/sse?toolId=defaults-object",
      "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]