如何在Jsonata中输出数组的序号与对应值?
问题:如何修改Jsonata表达式实现带序号的价格输出?
给定JSON数据
{ "Account": { "Account Name": "Firefly", "Order": [ { "OrderID": "order103", "Product": [ { "Price": 34.45, "Quantity": 2 }, { "Price": 21.67, "Quantity": 1 } ] } ] } }
当前Jsonata代码
(Account.Order.Product.(Price))
实际输出
[ 34.45, 21.67 ]
期望输出
1 : 34.45 2: 21.67
修改后的Jsonata表达式
$join(Account.Order.Product.(($index() + 1) & ": " & Price), "\n")
说明
$index():获取元素在数组中的起始索引(从0开始),加1后得到从1计数的序号& ": " & Price:将序号、分隔符与价格拼接成单行字符串$join(..., "\n"):用换行符把所有单行字符串连接起来,生成符合期望的文本格式
内容的提问来源于stack exchange,提问作者Shree
相关产品推荐
相关产品推荐

