Skip to content

JSLT Help: Combine 2 nodes from JSON Input #374

@naggupta

Description

@naggupta

I'm trying to get a list of all stock items along with their corresponding product details. Can anyone help me with this?

Here’s the sample input I'm working with:

{
"products": [
{ "productId": "A1", "price": 10, "category": "Fruit", "brand": "FreshCo" },
{ "productId": "B2", "price": 20, "category": "Veg", "brand": "GreenFarm" }
],
"stock": [
{ "store": { "storeId": "S1" }, "productId": "A1", "available": 5 },
{ "store": { "storeId": "S2" }, "productId": "B2", "available": 8 }
]
}
And here’s the JSLT code I’ve written so far:

let lookupProduct = function(productId)
[for (.products)
if (.productId == $productId)
{
"price": .price,
"category": .category,
"brand": .brand
}]

[
for (.stock)
let product = lookupProduct(.productId)
{
"storeId": .store.storeId,
"productId": .productId,
"available": .available,
"price": $product[0].price,
"category": $product[0].category,
"brand": $product[0].brand
}
]

Would appreciate any help. Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    supportUsers asking how to solve a specific issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions