0

So im trying to add a table inside another table but each time i do it, it adds a "1": from nowhere...

my code :

local previousClothes = json.decode(xPlayer.get('clothes'))
print("old previousClothes"..json.encode(previousClothes))

local clothes = {[label] = {[parentName] = parentValue, [partName] = partValue}}
print("old clothes"..json.encode(clothes))

clothes[#clothes+1] = previousClothes
print("new clothes: "..json.encode(clothes))

xPlayer.get('clothes') = my clothes stored in my db

local clothes = my new clothes received in the function/event

and here comes my issue.. it adds a "1": to my table

https://i.sstatic.net/sb5pj.png

3
  • Your clothes is not an array. It contains key-value entries. So, JSON treats it as dictionary (object) instead of as array. Commented Apr 10, 2020 at 11:37
  • Egor, you know how can i resolve this ? :/ Commented Apr 10, 2020 at 11:39
  • I don't see a problem here. Lua tables can be arrays and dictionaries simultaneously. JSON objects can not. Why do you use JSON repesentation to display Lua tables? Commented Apr 10, 2020 at 12:46

2 Answers 2

1

Instead of adding previousClothes as an array element to clothes, you can copy key-value pairs of previousClothes into clothes.

for k, v in pairs(previousClothes) do
    clothes[k] = v
end

I assume this is what you want.

Sign up to request clarification or add additional context in comments.

Comments

1

Because Your clothes is not an array, see the documentation in here. When you use # get a table length, it is better to be an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.