How to read a JSON file using Nix?

Viewed 557

How can I read a JSON file that exists in the filesystem with Nix? Is this natively supported?

1 Answers

If we have a file example.json like:

{
  "rev": "fcc9a7714053acb1aaf6913b99b6f49e0d13b1b7"
}

We can use the following where fromJSON will return an attribute set:

nix-repl> v = builtins.fromJSON (builtins.readFile "/path/to/example.json")        
nix-repl> v.rev
"fcc9a7714053acb1aaf6913b99b6f49e0d13b1b7"
Related