0
{
        "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] –
5
  • 2
    What does log.info(resp) return, and what is your error message?
    – Kate Paulk
    Commented Mar 4, 2019 at 18:52
  • Your JSON Object ends with a "]" - a spelling mistake or an error in your input? Commented Mar 5, 2019 at 6:27
  • that is missed by me the json Starts with "[" Commented Mar 5, 2019 at 8:40
  • @ThamilvendanPerumal could you fix the JSON in your question to correct? Commented Mar 5, 2019 at 12:17
  • What error did you get? Commented Mar 5, 2019 at 13:17

2 Answers 2

1

I think you have the logic wrong

JSON.dump creates a string while JSON.loads creates an object

so you are creating a string from json object and then converting it back to json object

and also you are calling resp['unavailableQty'], but if the resp is the json that you have given, then it does not have a direct property field 'unavailableQty'.

To get it ,

you can directly call

newJson3[0].unavailableLines.unavailableLine[0].unavailableQty

or in the same code,

resp[0].unavailableLines.unavailableLine[0].unavailableQty

Note:

There are two fields with unavailableQty

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:

3
  • 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 Commented Mar 5, 2019 at 12:53
  • Ok. Could you say what error did you get? Commented Mar 5, 2019 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] Commented Mar 5, 2019 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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