Course/Chapter 5/1. JSON Transformation Patterns
    intermediate
    Chapter 5: Data Transformation & Manipulation

    1. JSON Transformation Patterns

    Reshape JSON objects for downstream nodes.

    15m Lesson 1 of 5

    Real-world APIs return data in all shapes. Transforming JSON to match your target system's format is one of the most common tasks in automation.

    Common Transformations

    • - Rename fields: Map source fields to target field names
    • - Flatten nested objects: Pull nested values to the top level
    • - Restructure arrays: Convert arrays of objects to different shapes
    • - Merge fields: Combine multiple fields into one

    Code Examples

    Flatten Nested Object
    javascript
    // Input: { user: { name: "John", address: { city: "NYC" } } }
    // Output: { userName: "John", userCity: "NYC" }
    
    return $input.all().map(item => ({
      json: {
        userName: item.json.user.name,
        userCity: item.json.user.address.city,
        userEmail: item.json.user.email
      }
    }));

    Pro Tips

    • 💡Use the Set node for simple field mapping — save the Code node for complex transformations

    Comprehension Quiz

    Answer 2 of 2 correctly to unlock lesson completion.

    1. What is "flattening" a JSON object?

    2. Which node is best for simple field mapping?

    Pass the quiz above to unlock lesson completion