385i
385i

Reputation: 21

Lua - getting nested table element

i have JSON responce like this:

{"ts":"1026","updates":

[{"type":"message_new","object":{"message":

{"date":1588966108,"from_id":329211115,"id":0,"out":0,"peer_id":2000000003,"text":"test"}}},

{"type":"message_new","object":{"message": 

{"date":1588966109,"from_id":329211115,"id":0,"out":0,"peer_id":2000000003,"text":"test2"}}}]}

How to print an "text" field?

I have this responce converted to Lua table.

I tried to call

answer["updates"]["object"]["message"]["text"]

, but i had error 'attempt to index a nil value (field 'object')'

Upvotes: 0

Views: 569

Answers (1)

lhf
lhf

Reputation: 72412

updates is an array. So use

answer["updates"][1]["object"]["message"]["text"]

or

answer.updates[1].object.message.text

Upvotes: 2

Related Questions