Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@
- [`envBytes`](./cheatcodes/env-bytes.md)
- [`parseJson`](./cheatcodes/parse-json.md)
- [`keyExists`](./cheatcodes/key-exists.md)
- [`parseJsonKeys`](./cheatcodes/parse-json-keys.md)
- [`serializeJson`](./cheatcodes/serialize-json.md)
- [`writeJson`](./cheatcodes/write-json.md)
- [Utilities](./cheatcodes/utilities.md)
Expand Down
2 changes: 2 additions & 0 deletions src/cheatcodes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ interface CheatCodes {
function parseJson(string memory json) external returns (bytes memory);
// Check if a key exists in a json string
function keyExists(string memory json, string memory key) external returns (bytes memory);
// Get list of keys in a json string
function parseJsonKeys(string memory json, string memry key) external returns (string[] memory);

// Snapshot the current state of the evm.
// Returns the id of the snapshot that was created.
Expand Down
29 changes: 29 additions & 0 deletions src/cheatcodes/parse-json-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## `parseJsonKeys`

### Signature

```solidity
// Get list of keys present in a JSON string
function parseJsonKeys(string calldata json, string calldata key) external pure returns (string[] memory keys);
```

### Description

Gets list of keys present in a JSON string

### Examples

```solidity
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a few more examples on how to use this? particularly on the second param

string memory json = '{"key": {"a": 1, "b": 2}}';
string[] memory keys = vm.parseJsonKeys(json, ".key"); // ["a", "b"]
```

```solidity
string memory json = '{"key": "something"}';
string[] memory keys = vm.parseJsonKeys(json, "."); // ["key"]
```

```solidity
string memory json = '{"root_key": [{"a": 1, "b": 2}]}';
string[] memory keys = vm.parseJsonKeys(json, ".root_key.0"); // ["a", "b"]
```