question

WillUK avatar image
WillUK asked cwhitty edited

Passing JSON from itest into python command

I have a device returning perfectly valid JSON, however when it seems to get grabbed by ITEST it escapes it and miss format it.

I basically just want to save a json response and pass it into a python session, What is the best way to do this? I was dealing with the escape characters by just using .replace but now for some reason not only is there \ on every " it randomly throws in {} which cant be fixed with .replace.

Is there just a simple escape method I can just use on the variable when passing python the argument?

Thanks in advance.

iTestpythonjson
1 comment
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

WillUK avatar image WillUK commented ·

Seems like I just need to escape new lines, not sure how thats done in itest.

0 Likes 0 ·
cwhitty avatar image
cwhitty answered

Are you running the curl command from a terminal session like Windows command? If so, it sounds like you are seeing a long, single line response being broken into multiple lines whose length is equal to the step's terminal width (Command Prompt Session Properties / Command / Terminal / Size). You could either widen the terminal width or consider using the process session instead. Below is an example of using the process session to run curl. But, of course, the best option is to use the REST session, especially after the latest 8.0 improvements including Swagger 2.0 / OpenAPI 3.0, variable set and get, and interactive capture.


10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

bogdan_raicu avatar image
bogdan_raicu answered

In order to save the JSON output only, you will need to use "Store only the value of the response" from Step Properties / Other Post-processing / Store Response.

I did a similar test with and without the checkbox, and it is saving the correct JSON (when option is checked)

,

What Python version do you use? And can you send some screenshots?


1.png (38.1 KiB)
4.png (69.2 KiB)
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

WillUK avatar image
WillUK answered WillUK edited

Latest python, the issue seems to be when using Curl and storing the response, in compresses it down and enforces the restrictions of the box, so for example without even touching the data and purely just writing to file using the itest write to file, it looks like this.

Adds in white space at the end and splits up closing "'s

Also, when I save the valid response to a variable and pass it to python, it throws an error exactly at "goplenState which just so happens to be the start of the new line. If I read this file (written file) in a external python script and run regex to remove all white spaces and new lines, it becomes valid json again. So its purely the format side, but ideally we would want to avoid running a external script on this.

Not sure why curl or itest is basically taking this valid json and forcing it into some width requirement.


capture.png (21.2 KiB)
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

bogdan_raicu avatar image
bogdan_raicu answered

Hi @WillUK,

Please file a new Spirent Support case on CSC so that we can proceed with scheduling a debug call. This would help speeding the things up in order to achieve a faster resolution.

Once the issue is fixed I will reply to this thread with the accepted solution.

Thanks,

Bogdan

10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

WillUK avatar image
WillUK answered WillUK edited

I've found work arounds for the issues above, what puzzles me now though is how do I make it so when passing a json object as a argument via command prompt to python script, how do I make it so it just gives what I send? Its removing every mention of " so it malforms it.

10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

cwhitty avatar image
cwhitty answered cwhitty edited

This example shows passing JSON as a string into a Python script. Once the string is in Python, you can use use json.loads() to deserialize the string and treat it as a structure.

However, iTest 8.0 now allows you to pass native Python structures around into and out of procedures. So, consider the following example where a JSON string is created, it's then converted to a dictionary, passed to a procedure, modified by the procedure, then serialized again in the main body of the script. The part to note here is the 'store response' property, where the returned dictionary is stored as a native Python structure.


10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.