1
{
        "service": "CURBSIDE",
        "unavailableLines": {
            "unavailableLine": [
                {
                    "unavailableQty": "10.0",
                    "itemLineId": "4",
                    "unavailableReasonCode": "NOT_ENOUGH_PRODUCT_CHOICES"
                },
                {
                    "unavailableQty": "5.0",
                    "itemLineId": "3",
                    "unavailableReasonCode": "NOT_ENOUGH_PRODUCT_CHOICES"
                }
            ]
        },
        "id": "HD2",
        "deliveries": {
            "delivery": [
                {
                    "serviceItemId": "SGR40400002",
                    "deliveryId": "1",
                    "unitOfMeasure": "ORDERS"
                },
                {
                    "serviceItemId": "SGR40400003",
                    "deliveryId": "2",
                    "unitOfMeasure": "ORDERS"
                },
                {
                    "serviceItemId": "SGR40400002",
                    "deliveryId": "3",
                    "unitOfMeasure": "ORDERS"
                }
            ]
        },
        "earliestShipDate": "2018-12-03T07:00:00.000Z"
    }
]

The above is an JSON Object. I am trying to extract the values by using the below code in JSR223 processor:

//dumps the json object into an element
json_str = json.dumps(newJson3)

//load the json to a string
resp = json.loads(json_str)
//print the resp
log.info(resp)

//extract an element in the response
print (resp['unavailableQty'])

I am getting error response, kindly some one help me to reterive all the values from the Json Object

Find the error that I am getting while using the below script

json_str = json.dumps(newJson3)
resp = json.loads(json_str)
log.info(resp)
print (resp['unavailableQty']) 

Error:

javax.script.ScriptException: 
========Error================================================
javax.script.ScriptException: groovy.lang.MissingPropertyException: No such 
property: json for class: Script622 at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158) ~[groovy-all-2.4.12.jar:2.4.12] at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_171] –
  • 2
    What does log.info(resp) return, and what is your error message? – Kate Paulk Mar 4 at 18:52
  • Your JSON Object ends with a "]" - a spelling mistake or an error in your input? – Dakshinamurthy Karra Mar 5 at 6:27
  • that is missed by me the json Starts with "[" – Thamilvendan Perumal Mar 5 at 8:40
  • @ThamilvendanPerumal could you fix the JSON in your question to correct? – Vadim Yangunaev Mar 5 at 12:17
  • What error did you get? – Vadim Yangunaev Mar 5 at 13:17
0

Use JSON Extractor. Add this Post Pricessor as a child of request that returns JSON.

Specify:

  • Names of created variables: yourVariableName
  • JSON Path Expression: $.[0].unavailableLines.unavailableLine[0].unavailableQty
  • Match no: 1

To get first unavailableQty use this JSON Path:

$.[0].unavailableLines.unavailableLine[0].unavailableQty

To get all unavailableQty use *:

$.[0].unavailableLines.unavailableLine[*].unavailableQty

Check this:

  • Hi Thanks for your response, the above is not the response, it is an Json Object I am getting from an third party system so now i have to extract all the values inside the object – Thamilvendan Perumal Mar 5 at 12:53
  • Ok. Could you say what error did you get? – Vadim Yangunaev Mar 5 at 13:19
  • 1
    Pls find the error that I am getting while using the below script json_str = json.dumps(newJson3) resp = json.loads(json_str) log.info(resp) print (resp['unavailableQty']) javax.script.ScriptException: ========Error================================================javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: json for class: Script622 at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:158) ~[groovy-all-2.4.12.jar:2.4.12] at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_171] – Thamilvendan Perumal Mar 5 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.