问题
Below is the JSON file:
{
"_id": "rezbase_v3",
"_rev": "3-096e751caeeda12a8c30e28075a95e97",
"exchangerate_data": [
{
"buyingrate": 0.274725274725275,
"sellingrate": 0.274725274725275,
"buyingrateinverse": 3.64,
"sellingrateinverse": 3.64,
"basecurrency": {
"currencyCode": "USD",
"currencyName": "US dollar"
},
"quotecurrency": {
"currencyCode": "QAR",
"currencyName": "Qatari riyal"
},
"updateddate": 1491182700431,
"basecurrencyname": "US dollar",
"quotecurrencyname": "Qatari riyal"
},
{
"buyingrate": 0.00681384573453257,
"sellingrate": 0.00681384573453257,
"buyingrateinverse": 146.76,
"sellingrateinverse": 146.76,
"basecurrency": {
"currencyCode": "USD",
"currencyName": "US dollar"
},
"quotecurrency": {
"currencyCode": "LKR",
"currencyName": "Sri Lankan rupee"
},
"updateddate": 1491182700431,
"basecurrencyname": "US dollar",
"quotecurrencyname": "Sri Lankan rupee"
}
]
}
And from the file I need to filter buyingrate where basecurrency/currencycode is USD and quotecurrency/currencycode is LKR.
I have tried below expression:
<property description="quote1" expression="//jsonObject/exchangerate_data[basecurrency/currencyCode='USD' and quotecurrency/currencyCode='LKR']/buyingrate/text()" name="quote1" scope="default" type="STRING"/>
And I could be able to get the buyingrate.
But when I make the values as properties and tried to get the buyingrate, It is logging the exact expression insstead of the value.
<property description="basecurrency" name="basecurrency" scope="default" type="STRING" value="USD"/>
<property description="quotecurrency" name="quotecurrency" scope="default" type="STRING" value="LKR"/>
<property name="apos" scope="default" type="STRING" value="'"/>
<property description="exchangeratePath" expression="fn:concat(get-property('exchangeratePath'),'//jsonObject/exchangerate_data[basecurrency/currencyCode=',get-property('apos'),get-property('basecurrency'),get-property('apos'),' and quotecurrency/currencyCode=',get-property('apos'),get-property('quotecurrency'),get-property('apos'),']/buyingrate/text()')" name="exchangeratePath" scope="default" type="STRING"/>
<log level="custom">
<property expression="get-property('exchangeratePath')" name="exchangeratePath"/>
</log>
Logged value:
[2018-04-27 08:50:41,314] INFO - LogMediator exchangeratePath = //jsonObject/exchangerate_data[basecurrency/currencyCode='USD' and quotecurrency/currencyCode='LKR']/buyingrate/text()
I have checked tried the way mentioned in this answer.
回答1:
This is resolved by:
<property description="exchangeratePath" expression="fn:concat(get-property('exchangeratePath'),'//jsonObject/exchangerate_data[basecurrency/currencyCode=',get-property('apos'),get-property('basecurrency'),get-property('apos'),' and quotecurrency/currencyCode=',get-property('apos'),get-property('quotecurrency'),get-property('apos'),']/buyingrate/text()')" name="exchangeratePath" scope="default" type="STRING"/>
<property description="exchangeRate" expression="evaluate($ctx:exchangeratePath)" name="exchangeRate" scope="default" type="STRING"/>
来源:https://stackoverflow.com/questions/50042884/filter-data-from-json-by-checking-condition-with-two-properties-wso2-esb