inspec/docs/resources/json.md.erb

74 lines
1.5 KiB
Text
Raw Normal View History

2016-09-22 12:43:57 +00:00
---
title: About the json Resource
platform: os
2016-09-22 12:43:57 +00:00
---
# json
Use the `json` InSpec audit resource to test data in a JSON file.
<br>
## Availability
### Installation
This resource is distributed along with InSpec itself. You can use it automatically.
### Version
This resource first became available in v1.0.0 of InSpec.
## Syntax
2016-09-22 12:43:57 +00:00
A `json` resource block declares the data to be tested. Assume the following JSON file:
{
"name" : "hello",
"meta" : {
"creator" : "John Doe"
},
"array": [
"zero",
"one"
]
}
This file can be queried using:
describe json('/path/to/name.json') do
2016-09-22 12:43:57 +00:00
its('name') { should eq 'hello' }
its(['meta','creator']) { should eq 'John Doe' }
its(['array', 1]) { should eq 'one' }
end
where
* `name` is a configuration setting in a JSON file
* `should eq 'foo'` tests a value of `name` as read from a JSON file versus the value declared in the test
<br>
2016-09-22 12:43:57 +00:00
## Examples
2016-09-22 12:43:57 +00:00
The following examples show how to use this InSpec audit resource.
2016-09-22 12:43:57 +00:00
### name
The `name` matcher tests the value of the filename as read from a JSON file versus the value declared in the test:
its('name') { should eq '/tmp/example.json' }
### Test a cookbook version in a policyfile.lock.json file
2016-09-22 12:43:57 +00:00
describe json('policyfile.lock.json') do
its(['cookbook_locks', 'omnibus', 'version']) { should eq('2.2.0') }
end
2016-09-22 12:43:57 +00:00
<br>
2016-09-22 12:43:57 +00:00
## Matchers
2016-09-22 12:43:57 +00:00
For a full list of available matchers, please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).
2016-09-22 12:43:57 +00:00