diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 4b8840d23..1a80d04da 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/cheatcodes/README.md b/src/cheatcodes/README.md index 779cb4db4..aea5023d0 100644 --- a/src/cheatcodes/README.md +++ b/src/cheatcodes/README.md @@ -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. diff --git a/src/cheatcodes/parse-json-keys.md b/src/cheatcodes/parse-json-keys.md new file mode 100644 index 000000000..1614a7499 --- /dev/null +++ b/src/cheatcodes/parse-json-keys.md @@ -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 +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"] +``` \ No newline at end of file