Unable to fetch JSON ARRAY elements by using jsonpath/ xpath in WSO2 EI / MI

I have sample JSON Payload shared below.

{
              "multisetAttribute": [
                {
                  "attributes": [
                    {
                      "name": "Loading/Discount Type",
                      "value": "Bonus Discount"
                    },
                    {
                      "name": "Premium of Cover",
                      "value": "9550"
                    },
                    {
                      "name": "LD Rate",
                      "value": "0.35"
                    },
                    {
                      "name": "LD Amount",
                      "value": "3343"
                    }
                  ]
                },
                {
                  "attributes": [
                    {
                      "name": "Loading/Discount Type",
                      "value": "Renewal Loading - OD"
                    },
                    {
                      "name": "Premium of Cover",
                      "value": "1862"
                    },
                    {
                      "name": "LD Rate",
                      "value": "0.0"
                    },
                    {
                      "name": "LD Amount",
                      "value": "0"
                    }
                  ]
                },
                {
                  "attributes": [
                    {
                      "name": "Loading/Discount Type",
                      "value": "Experience Based Discount"
                    },
                    {
                      "name": "Premium of Cover",
                      "value": "6208"
                    },
                    {
                      "name": "LD Rate",
                      "value": "0.7"
                    },
                    {
                      "name": "LD Amount",
                      "value": "4345"
                    }
                  ]
                }
              ],
              "name": "Loading Discount Details",
              "value": null
            }

From above payload, I am trying to fetch “LD Rate” value which resides inside inner JSON Array named as “attributes”.

Since we have multiple “attributes” inner JSON Array , to fetch “LD Rate” element there is one specific condition ie In which compartment “Loading/Discount Type” value comes as “Experience Based Discount”, from that compartment, I should fetch “LD Rate” value (in this case 0.7)

Note: we should not pass array position to fetch element , Because this is dynamic one, we may receive more “attributes” json array.

Below xpath I tried.

<property expression="//multisetAttribute[*]/attributes[*]/name[text()='Loading/Discount Type']/../value[text()='Experience Based Discount']/text()" name="LD_Rate" scope="default" type="STRING"/>

Till this point as per above expression it is printing like “Experience Based Discount” as LD_Rate. really not sure how to navigate to next element value (ie LD Rate) in same node.

Tried below xpath expression which is not giving “LD Rate” value.

<property expression="//multisetAttribute[*]/attributes[*]/name[text()='Loading/Discount Type']/../value[text()='Experience Based Discount']/../name[text()='LD Rate']/../value/text()" name="LD_Rate" scope="default" type="STRING"/>

Sample API Code:

<?xml version="1.0" encoding="UTF-8"?>
<api context="/jsonarrayapi" name="jsonarrayapi"
    xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST">
        <inSequence>
            <property expression="//multisetAttribute[*]/attributes[*]/name[text()='Loading/Discount Type']/../value[text()='Experience Based Discount']/../name[text()='LD Rate']/../value/text()" name="LD_Rate" scope="default" type="STRING"/>
            <log level="custom">
                <property expression="//multisetAttribute[*]/attributes[*]/name[text()='Loading/Discount Type']/../value[text()='Experience Based Discount']/../name[text()='LD Rate']/../value/text()" name="LD_Rate" scope="default" type="STRING"/>
            </log>
            <payloadFactory media-type="json">
                <format>"LD_Rate":"$1"}</format>
                <args>
                    <arg evaluator="xml" expression="$ctx:LD_Rate"/>
                </args>
            </payloadFactory>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

Can someone help us to find out XPATH or JSON path expression for this issue?

0

This should get you all of the values.

Direct approach:

$.multisetAttribute.*.attributes[[email protected] == 'LD Rate'].value

Recursive search:

$..[[email protected] == 'LD Rate'].value

The direct approach will get only the nodes at that specific location in the tree.

The recursive approach will get you all of the LD Rate nodes, regardless of where they appear in the hierarchy.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật