0

EDIT: Take a lua table as a string, and use javascript to convert it to a javascript array. No programming in lua.

So a lua table is just an associative array in a different format.

    --LUA TABLE EXAMPLE
    {
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
    }

I've looked around for some javascript functions or libraries to do this though I've only come across some lua libraries to convert json to a lua table. The lua table will always be in a string. Does there exist a way of doing this?

3
  • Possible duplicate of Convert JSON String to Lua Table? Commented Sep 25, 2019 at 20:13
  • @VLAZ no i've already read that one, i'm looking to convert using JAVASCRIPT. Commented Sep 25, 2019 at 20:21
  • I've written a NPM package that does this. It's called Luon (for "Lua object notation"). Commented Sep 14, 2022 at 13:39

4 Answers 4

1

This is only a partial solution. It could fail in some cases where the text inside a string matches the key syntax. But that may not be a concern for you.

const lua = `
{
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
    }
}`

const lua2json = lua => 
  JSON .parse (lua .replace (
    /\[([^\[\]]+)\]\s*=/g, 
    (s, k) => `${k} :`
  ) 
  .replace (/,(\s*)\}/gm, (s, k) => `${k}}`))

console .log (
  lua2json (lua)
)

I didn't know if you were looking to create JSON or an object. I chose the latter, but you could always remove the JSON.parse wrapper.

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

2 Comments

Would not be better to include double quotes in regexp too ? Suppose it should be part of syntax and can give better reliability(?)
@Tom: Quite possibly. The problem is that this technique is inherently fragile. Any plain-regex based solution is almost certainly going to have failure modes, so the question is whether you want to use a full-fledged parser (I really recommend Peg.js) and if not, what compromises do you make. Since it looks like the quotes are required syntax for Lua (is this true?), and I was only focused on interpreting a well-formed Lua table string, I didn't worry about it. But that might help catch ill-formed input.
1

You can to do it manually to make it a valid JSON then parse it :

const luaStr = `{
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
    }}`;

const result = luaStr  
  .replace(/\[|\]/g, '') // remove the brackets
  .replace(/=/g, ':') // replace the = with :
  .replace(/(\,)(?=\s*})/g, ''); // remove trailing commas
  
const parsed = JSON.parse(result);

console.log(result);

5 Comments

What happens if the white space is in the middle of a string?'
@ScottSauyet oh, didn't see that, i removed them to make it easier to remove trailing commas, i'll rethink this
I only noticed because I was working on a somewhat similar solution (now an answer) that has a different failure mode inside a string!
@ScottSauyet i updated my answer, and yeah yours looks good but i guess there always will be tricky if the string had a value like "aaa", } for some reason ..
Yes, I think any regex-based solution is going to have such issues. This actually needs a proper parser. I think mine has a less-likely failure mode than this answer, but nothing like this will solve the general case.
0

Here is something that you might not know, { ["someString"]: 2 } is valid javascript and will evaluate to {someString: 2} which is valid json. The only issue is it needs to be evaluated, which means using eval (which you really never should, if you can avoid it).

const luaStr = `{
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
    }}`;
    
const jsonString = luaStr.replace(/\] = /g, ']: ');
const jsonObj = eval(`(${jsonString})`);

console.log(jsonObj);

Comments

0

Added array converting

const luaStr = `{
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["test1"] = { "test1"
     },

        ["test2"] = { "test1",
         "test2" },

        ["test3"] = { "test1", "test2", "test2" },
    }}`;

const result = luaStr  
.replace(/\[|\]/g, '') // remove the brackets
.replace(/=/g, ':') // replace the = with :
.replace(/(\,)(?=\s*})/g, '') // remove trailing commas
.replace(/\{\s*(\"[^".]*\"(\s*\,\s*\"[^".]*\")*)\s*\}/g, '[$1]') // to array

console.log (result)

const parsed = JSON.parse(result);
console.log(parsed);

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.