The current documentation for the JSON cheats does not explain that the key parameter must be provided with a leading dot:
|
// |
|
// parseJson |
|
// |
|
// ---- |
|
// In case the returned value is a JSON object, it's encoded as a ABI-encoded tuple. As JSON objects |
|
// don't have the notion of ordered, but tuples do, they JSON object is encoded with it's fields ordered in |
|
// ALPHABETICAL order. That means that in order to successfully decode the tuple, we need to define a tuple that |
|
// encodes the fields in the same order, which is alphabetical. In the case of Solidity structs, they are encoded |
|
// as tuples, with the attributes in the order in which they are defined. |
|
// For example: json = { 'a': 1, 'b': 0xa4tb......3xs} |
|
// a: uint256 |
|
// b: address |
|
// To decode that json, we need to define a struct or a tuple as follows: |
|
// struct json = { uint256 a; address b; } |
|
// If we defined a json struct with the opposite order, meaning placing the address b first, it would try to |
|
// decode the tuple in that order, and thus fail. |
|
// ---- |
|
// Given a string of JSON, return it as ABI-encoded |
|
function parseJson(string calldata json, string calldata key) external pure returns (bytes memory abiEncodedData); |
|
function parseJson(string calldata json) external pure returns (bytes memory abiEncodedData); |
|
|
|
// The following parseJson cheatcodes will do type coercion, for the type that they indicate. |
|
// For example, parseJsonUint will coerce all values to a uint256. That includes stringified numbers '12' |
|
// and hex numbers '0xEF'. |
|
// Type coercion works ONLY for discrete values or arrays. That means that the key must return a value or array, not |
|
// a JSON object. |
|
function parseJsonUint(string calldata, string calldata) external returns (uint256); |
I was trying to access the key by its simple name, like so:
string memory json = '{"bar":"Hello World"}';
string memory bar = vm.parseJsonString(json, "bar");
And that didn't work until I added the leading dot, i.e. .bar.
Side note: the parameters for the parseJsonSOMETHING cheats have been left unnamed.
The current documentation for the JSON cheats does not explain that the
keyparameter must be provided with a leading dot:forge-std/src/Vm.sol
Lines 213 to 239 in e8a047e
I was trying to access the key by its simple name, like so:
And that didn't work until I added the leading dot, i.e.
.bar.Side note: the parameters for the
parseJsonSOMETHINGcheats have been left unnamed.