I'm working on a REST API client in Lua that consumes data from a third-party service. The service returns deeply nested JSON objects, and I need to map these to more simplified Lua tables for easier manipulation and data analysis. I've explored using libraries like cjson for basic deserialization, but I'm struggling with mapping the nested JSON to my custom data structures efficiently.
Here's a simplified example of the JSON object returned by the API:
{
"data": {
"id": 1,
"attributes": {
"name": "John",
"details": {
"age": 30,
"address": {
"street": "123 Main St",
"city": "Anytown"
}
}
}
}
}
And here's what I would like to map this to in Lua:
Person = {
id = 1,
name = "John",
age = 30,
street_address = "123 Main St",
city = "Anytown"
}
Is there a best practice for this type of mapping? Are there any Lua libraries that can assist with this, or should I be writing custom mapping functions? I'm concerned about both code maintainability and performance.
Thank you in advance for any insights or recommendations!
The "traversing JSON" functionality is useful here.
Output:
The library is here