Ask AI
Skip to main content

Map to attributes (jsonpath)

Function: Map to attributes (jsonpath)

This action helps you transform a list of structured items (like customer records or product details) into a new list, where each item contains only specific pieces of information you've chosen. You define exactly which data points you want to extract from each original item using "JSONPaths," and you can even rename these extracted data points for clarity.

Input,

  • Array: The original list of items you want to process. Each item in this list should be a structured record (an object) containing various data fields.
  • Jsonpaths: A list of instructions that tell the system exactly which data to pull out from each item in your "Array." For each instruction, you provide:
    • Jsonpath: A precise path (like $.customerName or $.address.city) to locate a single piece of information within each item. This path must point to one specific attribute.
    • Field name: The name you want to give to the extracted piece of information in your new list. If you leave this blank, the system will try to create a name based on your Jsonpath.
  • Map to flat list: A switch (True/False) that changes how the output list is structured.
    • If set to True, and you've only provided one Jsonpath, the result will be a simple list of values (e.g., just a list of names).
    • If set to False (the default), or if you provide multiple Jsonpaths, the result will be a list of new structured items, each containing the specific data points you extracted.

Output,

  • Result: The name of the variable where the newly transformed list will be stored. By default, this variable is named RESULT.

Execution Flow,

Real-Life Examples,

Example 1: Extracting specific customer details

Imagine you have a list of customer records, and you only need their name and email address for a marketing campaign.

  • Inputs:

    • Array:
      [
      \{"id": "C001", "name": "Alice Smith", "email": "[email protected]", "phone": "555-1234"\},
      \{"id": "C002", "name": "Bob Johnson", "email": "[email protected]", "phone": "555-5678"\},
      \{"id": "C003", "name": "Charlie Brown", "email": "[email protected]", "phone": "555-9012"\}
      ]
    • Jsonpaths:
      • Jsonpath: $.name, Field name: CustomerName
      • Jsonpath: $.email, Field name: CustomerEmail
    • Map to flat list: False
    • Result variable name: MarketingContacts
  • Result: A new list named MarketingContacts will be created, containing:

    [
    \{"CustomerName": "Alice Smith", "CustomerEmail": "[email protected]"\},
    \{"CustomerName": "Bob Johnson", "CustomerEmail": "[email protected]"\},
    \{"CustomerName": "Charlie Brown", "CustomerEmail": "[email protected]"\}
    ]

Example 2: Getting a flat list of product prices

You have a list of products, and you want a simple list of all their prices to calculate an average.

  • Inputs:

    • Array:
      [
      \{"productId": "P101", "name": "Laptop", "price": 1200.00, "category": "Electronics"\},
      \{"productId": "P102", "name": "Mouse", "price": 25.50, "category": "Electronics"\},
      \{"productId": "P103", "name": "Keyboard", "price": 75.00, "category": "Electronics"\}
      ]
    • Jsonpaths:
      • Jsonpath: $.price, Field name: (Leave empty)
    • Map to flat list: True
    • Result variable name: ProductPrices
  • Result: A new list named ProductPrices will be created, containing:

    [1200.00, 25.50, 75.00]

Example 3: Extracting order item quantities and product names from nested data

You have a list of orders, and each order contains a list of items. You want to create a new list showing the product name and quantity for each individual item across all orders.

  • Inputs:

    • Array:
      [
      \{"orderId": "ORD001", "customer": "Alice", "items": [\{"product": "Shirt", "qty": 2\}, \{"product": "Pants", "qty": 1\}]\},
      \{"orderId": "ORD002", "customer": "Bob", "items": [\{"product": "Hat", "qty": 3\}]\}
      ]
    • Jsonpaths:
      • Jsonpath: $.items[*].product, Field name: ItemName
      • Jsonpath: $.items[*].qty, Field name: Quantity
    • Map to flat list: False
    • Result variable name: AllOrderItems
  • Result: A new list named AllOrderItems will be created. Note that because $.items[*] is used, it will extract all products and quantities from the nested array, creating a separate entry for each:

    [
    \{"ItemName": ["Shirt", "Pants"], "Quantity": [2, 1]\},
    \{"ItemName": ["Hat"], "Quantity": [3]\}
    ]

    Self-correction: The JsonPath library's read method with [*] will return an array for that path. The current implementation will put the entire array of products/quantities into the ItemName and Quantity fields for each order. If the goal was to flatten all individual items across orders, a different approach (e.g., a loop over items array first) would be needed. However, sticking to the describe\(\) method's JSONPATH which states "Select a single attribute from your JSON data", using $.items[*].product would actually return an array of products for each order. The example should reflect this.

Let's refine Example 3 to be more precise with "single attribute" rule. If we want individual items, we'd typically process the items array first. But if we stick to the "single attribute" rule for JSONPATH, then $.items[*].product is technically extracting one attribute which is an array of products.

Let's re-think Example 3 to better fit the "single attribute" rule and the expected output of the code. The code iterates for \(Object item : dataList\) and then applies jsonPaths to that single item. So, if dataList is a list of orders, each item is an order.

If JSONPATH is $.items[0].product, it would get the first product. If it's $.items[*].product, it would get an array of products. The description says "Select a single attribute... This field only accepts JSONPath queries that point to one attribute". This implies that $.items[*].product might not be the intended use if it returns an array. However, JsonPath.read can return an array if the path points to multiple elements.

Let's assume "single attribute" means the result of the JSONPath query should be a single value, or if it's an array, it's treated as a single attribute (the array itself). The code Object value = ctx.read\(jsonPath, new Predicate[0]\); will indeed return an array if jsonPath is $.items[*].product.

So, Example 3 is actually correct in its input and output interpretation given the code's behavior.

Final check on rules:

  • JSON in json ... : Yes.
  • High-level, non-technical: Yes.
  • No implementation details: Yes.
  • Invented realistic examples: Yes.
  • Based on describe\(\) method: Yes.
  • Sticks to input/output in describe\(\): Yes.
  • Correct Markdown: Yes.

Looks good.## Function: Map to attributes (jsonpath)

This action helps you transform a list of structured items (like customer records or product details) into a new list, where each item contains only specific pieces of information you've chosen. You define exactly which data points you want to extract from each original item using "JSONPaths," and you can even rename these extracted data points for clarity.

Input,

  • Array: The original list of items you want to process. Each item in this list should be a structured record (an object) containing various data fields.
  • Jsonpaths: A list of instructions that tell the system exactly which data to pull out from each item in your "Array." For each instruction, you provide:
    • Jsonpath: A precise path (e.g., $.customerName or $.address.city) to locate a single piece of information within each item. This path must point to one specific attribute; if you need multiple attributes, create separate mappings for each.
    • Field name: The name you want to give to the extracted piece of information in your new list. If you leave this blank, the system will try to create a name based on your Jsonpath.
  • Map to flat list: A switch (True/False) that changes how the output list is structured.
    • If set to True, and you've only provided one Jsonpath, the result will be a simple list of values (e.g., just a list of names).
    • If set to False (the default), or if you provide multiple Jsonpaths, the result will be a list of new structured items, each containing the specific data points you extracted.

Output,

  • Result: The name of the variable where the newly transformed list will be stored. By default, this variable is named RESULT.

Execution Flow,

Real-Life Examples,

Example 1: Extracting specific customer details

Imagine you have a list of customer records, and you only need their name and email address for a marketing campaign.

  • Inputs:

    • Array:
      [
      \{"id": "C001", "name": "Alice Smith", "email": "[email protected]", "phone": "555-1234"\},
      \{"id": "C002", "name": "Bob Johnson", "email": "[email protected]", "phone": "555-5678"\},
      \{"id": "C003", "name": "Charlie Brown", "email": "[email protected]", "phone": "555-9012"\}
      ]
    • Jsonpaths:
      • Jsonpath: $.name, Field name: CustomerName
      • Jsonpath: $.email, Field name: CustomerEmail
    • Map to flat list: False
    • Result variable name: MarketingContacts
  • Result: A new list named MarketingContacts will be created, containing:

    [
    \{"CustomerName": "Alice Smith", "CustomerEmail": "[email protected]"\},
    \{"CustomerName": "Bob Johnson", "CustomerEmail": "[email protected]"\},
    \{"CustomerName": "Charlie Brown", "CustomerEmail": "[email protected]"\}
    ]

Example 2: Getting a flat list of product prices

You have a list of products, and you want a simple list of all their prices to calculate an average.

  • Inputs:

    • Array:
      [
      \{"productId": "P101", "name": "Laptop", "price": 1200.00, "category": "Electronics"\},
      \{"productId": "P102", "name": "Mouse", "price": 25.50, "category": "Electronics"\},
      \{"productId": "P103", "name": "Keyboard", "price": 75.00, "category": "Electronics"\}
      ]
    • Jsonpaths:
      • Jsonpath: $.price, Field name: (Leave empty)
    • Map to flat list: True
    • Result variable name: ProductPrices
  • Result: A new list named ProductPrices will be created, containing:

    [1200.00, 25.50, 75.00]

Example 3: Extracting specific details from nested order items

You have a list of orders, and each order contains a list of items. You want to create a new list showing the product name and quantity for each individual item within each order.

  • Inputs:

    • Array:
      [
      \{"orderId": "ORD001", "customer": "Alice", "items": [\{"product": "Shirt", "qty": 2\}, \{"product": "Pants", "qty": 1\}]\},
      \{"orderId": "ORD002", "customer": "Bob", "items": [\{"product": "Hat", "qty": 3\}]\}
      ]
    • Jsonpaths:
      • Jsonpath: $.items[*].product, Field name: ProductsInOrder
      • Jsonpath: $.items[*].qty, Field name: QuantitiesInOrder
    • Map to flat list: False
    • Result variable name: OrderSummary
  • Result: A new list named OrderSummary will be created. For each order, the extracted product names and quantities will be collected into arrays:

    [
    \{"ProductsInOrder": ["Shirt", "Pants"], "QuantitiesInOrder": [2, 1]\},
    \{"ProductsInOrder": ["Hat"], "QuantitiesInOrder": [3]\}
    ]