JSON to Luau Table on require

hey let’s say I have a json string from a file like this

{
“files”: “require(script.Json)”
}

is there a way to decode it but turn the require into real require and not string anymore? Thanks!

to turn it into
{
files = require(script.Json)
}

instead of
{
files = “require(script.Json)”
}

Thanks!

Use HTTPService:DecodeJSON()

So, you would use HttpService:JSONDecode() to turn it in to a Luau table, then if the entire statement is a string, you can use loadstring(). Just be careful because loadstring() can then be used by exploiters if you enable it.

no, its not about loading it, it’s just a question to know whether it’s possible to convert “requiee(abc”) to require(abc)

it can convert “require(abc)” to require(abc)? see the difference.

Yes. If the code you want to run is in a string, you can use loadstring(), like I said. It returns the given string as a subprogram that can be executed.

local myCode = "print('testing 123')"
local executable = loadstring(myCode)
executable() --> "testing 123"

You need to enable loadstring in the properties of ServerScriptService. Bare in mind it can be a security hazard, because exploiters may use it to inject malicious code.

HttpService:JSONDecode was only to turn the JSON string into a Lua table. @ApparentlyJames also mentioned this, but got the name wrong.

1 Like

or maybe I should use GitHub - RealEthanPlayzDev/LuauCeption: Running Luau inside Luau? do you know about this?

but loadstring can use require(script.Parent.blabla) instead of direct function or url?

I don’t really understand what you’re asking anymore.

Do you want to return code that is executable, when the code is a string? That’s what loadstring is for.
Do you want to get code from an item that is not in your game? You can use require with an asset ID to require that module.

What are you trying to do?

No, I don’t know about that thing, sorry.

No, sadly, you can’t do that. Though you can transfer the directory of the module script by using Instance:GetFullName() which returns a string, which you can send to somewhere else and use it to trace back.

Well i wanna require it, but it’s from a module of string called ABC and a child of ABC is a module called Function1. if my path from json string module called “ABC” is “require(script.Function)” on loadstring does it look script.Function from the JSON string module or from script that execute the loadstring? does it means it’s == require(ABCModule.Function1) or require(scriptThatDoLoadString.Function1)

because the json string is stored inside a module called ABC, and the script where i do loadstring is from another serverscript, so if the string is “require(script.Function1)” during loadstring does it look Function1 module from the serverscript or from ABC module?

What you want to do is convert strings to lua code, the only built-in function for this is the loadstring function. It returns whatever the code you pass in returns. So you can do:

local files = loadstring(jsonData.files)()

print(files)

In order to use loadstring you must enable it through game.ServerScriptService.LoadStringEnabled = true that you can either run in the command line or tick after clicking on the ServerScriptService service/folder on the explorer.

For security reasons this setting is disabled by default because some backdoors use it to execute code, and the function can only be ran by a server script.

or maybe I can use uhm things like LuauCeption + Fiu?

If you like you can use custom loadstring modules and code execution libraries, although I can’t promise they will perfectly replicate the built-in loadstring behaviour.

but i wanna ask let’s say I have two scripts
1st Script (A.luau)

loadstring(require(script..Parent.B))()

2nd Script (B.luau)

return [[local a = require(script.Execute)
  return a
]]

Does it look script.Execute under A.luau or still under B.luau?

Note: It’s just a simple example because I’m on mobile