cancel
Showing results for 
Search instead for 
Did you mean: 

Set a JSONPath expression parameter from a datasource

SOLVED
Highlighted
Contributor

Set a JSONPath expression parameter from a datasource

How do I get a data source value for JSONPath Match assertion on the expression

 

Here is the compare values that gets compared, but the 0 is only valid for the first record.

JSONPath Expression

$['CalculatedOptions'][0]['DeathPayOption'][0]['OptionNum']

Expected Results
${DataSource#DO_OPTIONNUM}

 

What I would like to do is get the value from the datasource to replace the 0 value in ['DeathPayOption'][0] because there are multiple values of DeathPayOption

 

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: Set a JSONPath expression parameter from a datasource

Hi @cst99003,

 

Assuming that the datasource is set correctly, your assertion should work if you change the JSONPath expression to:

 

$.CalculatedOptions[${Datasource#CALCOPT_CNT}].RetirementDate

So:

1. You didn't place the dollar sign ( $ ) before the expression and

2. You put the quotes ( ' ' ) inside the square brakets which is wrong since this makes the value being taken as a string.



If my answer helped please click on the 'Accept as Solution' button.
11 REPLIES 11
Community Hero

Re: Set a JSONPath expression parameter from a datasource

Hey

Im struggling to understand the problem.

That zerovalue [0] in your assertion's expression is the jsonpath syntax. The first record starts with [0], 2nd record is [1], etc.

That json 'expression' is actually how json uses the path to navigate to and identify the first OptionNum node in your json response.....so there are actually >1 records in the generated response.

Are you saying that the OptionNum value saved in your datasource is not correct? Or it is, its just not the OptionNum value returned in the first record of your multi record response?

Sorry im not understanding....can you provide a bit more info?

There is a count assertion that counts the number of recorda returned in your response. You might be able to use this to help. Also...and i alwaya suggest this but iveonly ever managed to use this approach once. Depending on the api you are hitting and whether the request is a get, post etc., you could (this is a possibility only...ive only ever managed this once) tailor your requests template and query parameters do you actually alter the query that is executed....further restricting the number of records returned in the response from >1 to =1 only. As i say....ive only managed to alter the query filter on the REST request once....but it did allow me to retrieve only 1 record.

Cheers,

Richie
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Contributor

Re: Set a JSONPath expression parameter from a datasource

 Thank you for your response.

 

I am trying to confirm the datasource data matches the REST response.

 

The datasource from the database has one to many records so the value that I am looking for, in the compare is for ['OptionNum'] is the ['DeathPayOption'][1].  The DeathPayOption value is in the spreadsheet so i want to use that to get the correct attribute.

 

Assume I have two records in the datasource in two rows.  The

$['CalculatedOptions'][0]['DeathPayOption'][0]['Annuity']

${DataSource#ANNUITY} will work for the first record but because it looks at index = 0 but the second will fail because I need to look at index = 1.   I have a value in the datasource to tell me there are two records so i should be able to specify the index value for ['DeathPayOption'][0]

 

 

JSONPath Expression

$['CalculatedOptions'][0]['DeathPayOption'][0]['OptionNum']

Expected Results
${DataSource#DO_OPTIONNUM}

 

or......

$['CalculatedOptions'][0]['DeathPayOption'][0]['Annuity']

${DataSource#ANNUITY}

 

 

Sorry if my question is confuing but I assume I should be able to use the datasource to set the value.

 

Community Hero

Re: Set a JSONPath expression parameter from a datasource

Hey @cst99003

 

you'll have to forgive me - I'm quite literal so I'm struggling a little still.- my ADHD doesn't help me to concentrate either.

 

You mention database, datasource, spreadsheet

 

"The datasource from the database has one to many records" - are you using a datasource step object in your test case?  Can you please describe the steps in your test case just so I have a littel background?

 

From the mention of datasource and database - are you using a Data connection datasource object and using this to connect to a database?  If yes - can I assume you are using this to build the content of your request?

 

you mention " The DeathPayOption value is in the spreadsheet so i want to use that to get the correct attribute." - what spreadsheet?  I'm sorry - you never mentioned a spreadsheet before - are you using a spreadsheet as your datasource - or are you using a database?  Confused... Smiley Wink

 

Is it REST?  SOAP?

Are you doing a POST? or is it a GET?

 

 Essentially - you are doing some sort of REQUEST that is returning a .json body (so I'm assuming REST, not SOAP).  in this .json body you have >1 record returned.

 

You are saying one option fails because you can assert on $['CalculatedOptions'][0]['DeathPayOption'][0]['OptionNum'] but you also have a record  at $['CalculatedOptions'][0]['DeathPayOption'][1]['OptionNum'] - can you not just assert on this second instance of OptionNum to prove the value is correct?

 

It would help a lot if I knew what the content of your datasource is.  you mention you are trying to assert the OptionNum attribute against ${DataSource#DO_OPTIONNUM} - but I don't know what DO_OPTIONNUM actually is.

 

I'm sorry - some of other lads who are cleverer than me might be able to infer the content - but I need it laying out before I can visualise.

 

Thanks man,

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Contributor

Re: Set a JSONPath expression parameter from a datasource

Let me try to start again...  and sorry if I am incorrect on my terminology.

 

What I really need to do is create an assertion that will allow me to check the values in each node because the data in teh spreadsheet from the db is returning records based on those nodes. For DeathPayOption I know there is two (0,1) and this is seen in the response.

The data in the spreadsheet returns a row for each of the Annuity. Record 1 is AnnuityA and Record 2 is AnnuityB.  So I need to change the DeathPayOption on the fly. Perferably from a field in the spreadsheet. Ultimately this will make the Assert dynamic.

 

$['CalculatedOptions'][0]['DeathPayOption'][1][Annuity]

'CalculatedOptions'0

    'DeathPayOption'0 <--- I need to control this value from the spreadsheet

        AnnuityA

    'DeathPayOption'1

        AnnuityB

Community Hero

Re: Set a JSONPath expression parameter from a datasource

Hey im driving home. Will answer when i get back

Ta
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Community Hero

Re: Set a JSONPath expression parameter from a datasource

Where did your last response go? I saw it...ireplied saying i was driving and look when i got home....but now your post has disappeared!

Confused.....again! Smiley Wink
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the title of the post to something more descriptive? This will help people when searching for problems.
Contributor

Re: Set a JSONPath expression parameter from a datasource

Weird.  I will have to look in a bit

Contributor

Re: Set a JSONPath expression parameter from a datasource

See image.

 

I am trying to parameterize these value from the image

Community Hero

Re: Set a JSONPath expression parameter from a datasource

Hi @cst99003,

 

Assuming that the datasource is set correctly, your assertion should work if you change the JSONPath expression to:

 

$.CalculatedOptions[${Datasource#CALCOPT_CNT}].RetirementDate

So:

1. You didn't place the dollar sign ( $ ) before the expression and

2. You put the quotes ( ' ' ) inside the square brakets which is wrong since this makes the value being taken as a string.



If my answer helped please click on the 'Accept as Solution' button.
New Here?
Join us and watch the welcome video:
Top Kudoed Authors