How to modify, validate or read JSON files

Modification of JSON file content

If you have a JSON file that you would like to modify as part of a test case, you can follow the steps below:

1. Download the file frothis link

2. Upload the same file to Repositories > File Uploads

3. Go to Repositories > Code Snippets and create a new Code Snippet

4. Select the file you uploaded in step 2 from Dependencies

5. Next to Class, enter com.subject7.JsonModifier

6. Save the Code Snippet and go to your test case

7. Create 3 steps with the command Set_Var where each will use the following variable name:

  • JS_MODIFIER_FILE_PATH this is where you can add the path to the file. For example: C:/Users/Admin/Downloads/file.json
  • JS_MODIFIER_JSON_PATH  this is where you can specify the key you would like to modify in the json file. 
  • JS_MODIFIER_JSON_VALUE this is where you can add the new value that you will replace the existing one with

8. Create a step with Execute_Java command and select the snippet you created earlier

9. Next to Method, enter modify

10. You can now run your test to modify the JSON file

 

Validation of JSON file content

If you have a JSON file that you would like to validate and get a failure if the value does not match the key, you can follow the steps below:

1. Download the file from this link

2. Upload the same file to Repositories > File Uploads

3. Go to Repositories > Code Snippets and create a new Code Snippet

4. Select the file you uploaded in step 2 from Dependencies

5. Next to Class, enter com.subject7.JsonModifier

6. Save the Code Snippet and go to your test case

7. Create 3 steps with the command Set_Var where each will use the following variable name:

  • JS_MODIFIER_FILE_PATH this is where you can add the path to the file. For example: C:/Users/Admin/Downloads/file.json
  • JS_MODIFIER_JSON_PATH this is where you can specify the key you would like to validate
  • JS_MODIFIER_JSON_VALUE this is where you can add the value that the key should have

8. Create a step with Execute_Java command and select the snippet you created earlier

9. Next to Method, enter validate

10. You can now run your test to validate the JSON file

 

Reading JSON file content

If you have a JSON file that you would like to read as part of a test case, you can follow the steps below:

1. Download the file from this link

2. Upload the same file to Repositories > File Uploads

3. Go to Repositories > Code Snippets and create a new Code Snippet

4. Select the file you uploaded in step 2 from Dependencies

5. Next to Class, enter com.subject7.JsonModifier

6. Save the Code Snippet and go to your test case

7. Create 3 steps with the command Set_Var where each will use the following variable name:

  • JS_MODIFIER_FILE_PATH this is where you can add the path to the file. For example: C:/Users/Admin/Downloads/file.json
  • JS_MODIFIER_JSON_PATH  this is where you can specify the key you would like to modify in the json file. 

8. Create a step with Execute_Java command and select the snippet you created earlier

9. Next to Method, enter read

10. You can now run your test to read the JSON file. The value will be stored automatically to a variable with name JS_RESULT but you can also add a step with Set_Var command before Execute_Java step with variable name JS_RESULT_VARIABLE and the value can include your own variable name. This will override JS_RESULT into a variable name of your choosing.

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Please sign in to leave a comment.