Nim: How to parse nested JSON into objects
Mar 22, 2022
Nim will automatically handle recursive parsing of a JSON string into its constituent parts. If it's a nested object with specific types it can handle that too:
import jsontypeAddress = objectline: stringstate: stringcountry: stringPerson = objectfirst_name: stringlast_name: stringaddress: Addresslet jsonObject = parseJson("""{"first_name": "glenn","last_name": "gillen","address": {"line": "123 Main St","state": "VIC","country": "AU"}}""")let person = to(jsonObject, Person)echo person.first_name # glennecho person.address.state # VIC
Hi, I'm Glenn! 👋 I've spent most of my career working with or at startups. I'm currently the Director of Product @ Ockam where I'm helping developers build applications and systems that are secure-by-design. It's time we started securely connecting apps, not networks.
Previously I led the Terraform product team @ HashiCorp, where we launched Terraform Cloud and set the stage for a successful IPO. Prior to that I was part of the Startup Team @ AWS, and earlier still an early employee @ Heroku. I've also invested in a couple of dozen early stage startups.
Previously I led the Terraform product team @ HashiCorp, where we launched Terraform Cloud and set the stage for a successful IPO. Prior to that I was part of the Startup Team @ AWS, and earlier still an early employee @ Heroku. I've also invested in a couple of dozen early stage startups.