Can anyone tell me what format this uh.. nested dictionary is?

cross-posted from: lemmy.nz/post/4294116

I have a file with content like this:


<span style="color:#323232;">item({
</span><span style="color:#323232;">     ["attr"] = {
</span><span style="color:#323232;">        ["size"] = "62091";
</span><span style="color:#323232;">        ["filename"] = "qBuUP9-OTfuzibt6PQX4-g.jpg";
</span><span style="color:#323232;">        ["stamp"] = "2023-12-05T19:31:37Z";
</span><span style="color:#323232;">        ["xmlns"] = "urn:xmpp:http:upload:0";
</span><span style="color:#323232;">        ["content-type"] = "image/jpeg";
</span><span style="color:#323232;">     };
</span><span style="color:#323232;">     ["key"] = "Wa4AJWFldqRZjBozponbSLRZ";
</span><span style="color:#323232;">     ["with"] = "email@address";
</span><span style="color:#323232;">     ["when"] = 1701804697;
</span><span style="color:#323232;">     ["name"] = "request";
</span><span style="color:#323232;">});
</span>

I need to know what format this is, and if there exists a tool in linux already to parse this or if I need to write one myself?

Thanks!

kool_newt,

WTFON

56_,
@56_@lemmy.ml avatar

I think it’s just normal Lua code.

Here’s a quick json converter (based on stackoverflow.com/a/55575074), assuming you have lua installed:


<span style="font-weight:bold;color:#a71d5d;">local function </span><span style="font-weight:bold;color:#795da3;">to_json</span><span style="color:#323232;">(obj)
</span><span style="color:#323232;">    </span><span style="font-weight:bold;color:#a71d5d;">local</span><span style="color:#323232;"> result </span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;"> {}
</span><span style="color:#323232;">    </span><span style="font-weight:bold;color:#a71d5d;">for</span><span style="color:#323232;"> key, value </span><span style="font-weight:bold;color:#a71d5d;">in </span><span style="color:#62a35c;">pairs</span><span style="color:#323232;">(obj) </span><span style="font-weight:bold;color:#a71d5d;">do
</span><span style="color:#323232;">        </span><span style="font-weight:bold;color:#a71d5d;">if </span><span style="color:#62a35c;">type</span><span style="color:#323232;">(value) </span><span style="font-weight:bold;color:#a71d5d;">== </span><span style="color:#183691;">"string" </span><span style="font-weight:bold;color:#a71d5d;">then
</span><span style="color:#323232;">            value </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#62a35c;">string.format</span><span style="color:#323232;">(</span><span style="color:#183691;">"</span><span style="color:#0086b3;">"</span><span style="color:#183691;">%s</span><span style="color:#0086b3;">"</span><span style="color:#183691;">"</span><span style="color:#323232;">, value)
</span><span style="color:#323232;">        </span><span style="font-weight:bold;color:#a71d5d;">elseif </span><span style="color:#62a35c;">type</span><span style="color:#323232;">(value) </span><span style="font-weight:bold;color:#a71d5d;">== </span><span style="color:#183691;">"table" </span><span style="font-weight:bold;color:#a71d5d;">then
</span><span style="color:#323232;">            value </span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#323232;"> to_json(value)
</span><span style="color:#323232;">        </span><span style="font-weight:bold;color:#a71d5d;">end
</span><span style="color:#323232;">        </span><span style="color:#62a35c;">table.insert</span><span style="color:#323232;">(result, </span><span style="color:#62a35c;">string.format</span><span style="color:#323232;">(</span><span style="color:#183691;">"</span><span style="color:#0086b3;">"</span><span style="color:#183691;">%s</span><span style="color:#0086b3;">"</span><span style="color:#183691;">:%s"</span><span style="color:#323232;">, key, value))
</span><span style="color:#323232;">    </span><span style="font-weight:bold;color:#a71d5d;">end
</span><span style="color:#323232;">    </span><span style="font-weight:bold;color:#a71d5d;">return </span><span style="color:#183691;">"{" </span><span style="font-weight:bold;color:#a71d5d;">.. </span><span style="color:#62a35c;">table.concat</span><span style="color:#323232;">(result, </span><span style="color:#183691;">","</span><span style="color:#323232;">) </span><span style="font-weight:bold;color:#a71d5d;">.. </span><span style="color:#183691;">"}"
</span><span style="font-weight:bold;color:#a71d5d;">end
</span><span style="color:#323232;">
</span><span style="font-weight:bold;color:#a71d5d;">function </span><span style="font-weight:bold;color:#795da3;">item</span><span style="color:#323232;">(obj)
</span><span style="color:#323232;">    </span><span style="color:#62a35c;">print</span><span style="color:#323232;">(to_json(obj))
</span><span style="font-weight:bold;color:#a71d5d;">end
</span><span style="color:#323232;">
</span><span style="color:#62a35c;">dofile</span><span style="color:#323232;">(arg[</span><span style="color:#0086b3;">1</span><span style="color:#323232;">])
</span>

It just defines the item function to print json, and executes the data file.

arg[1], the first command line argument, is the path to the data file:


<span style="color:#323232;">$  lua to_json.lua path/to/datafile.list
</span>

and pipe the output to something.json or whatever else you want to do.

flubba86,

It’s not really a standalone file format, it’s executable Lua code.

It returns a new item with the given table contents.

That syntax with the keys in square brackets is the “long-form” method of creating a new table, that’s allows the use of spaces and dashes in the key name.

stackoverflow.com/…/what-is-the-function-of-squar…

Maybe this is the lua-equivelent of a python Pickle file?

luthis,

Ohhhhh…

Ok so I just have to write a bit of Lua to utilise the file and give me the info I want.

Thanks!

vrighter,

assuming you run it in the right lua environment. The item function must be defined, and we’re only speculating about its return value without seeing proper docs, or the source

luthis,

Item is a function?

Well actually, yeah thats kinda obvious isn’t it now I look at the whole thing.

Thats fine, I’ll just use a bit of the old sed and json it.

Aha I have avoided learning Lua yet again!

vrighter,

the code is constructing a table, and passing it to a function called item. But if all you need is the data, you can just remove the function call and assign the table to a variable like so: local myvar = {…}.

then you can just manipulate the table as usual.

luthis,

Unfortunately, this sequence is repeated many many times, so I would need to do a for-each and construct a new table for each inner section…

There’s gotta be a better way. Time to read the source code and hijack whatever item() is doing.

vrighter,

actually those semicolons indicate this isn’t actually lua, they are invalid in table constructors afaik

Jummit, (edited )

This isn’t Lua code, Lua requires commas as separators for table items.

EDIT: Retracted, it seems like Lua allows this madness

Celediel,
Jummit,

Wow. Seems like I will never stop learning new things about Lua.

Saganaki,

Looks like somebody rewrote json to require brackets around keys and to require semicolons? Very likely custom.

luthis,

The semi-colons threw me off… why is it not commas? Could be custom. Hope it isn’t…

draughtcyclist,

Almost looks like mongodb output. What’s the file extension?

luthis,

It’s .list

I believe the program that generated this file is written in Lua

syd,
@syd@lemy.lol avatar

WTH is this shit? @postwatchbot

Discover5164,

lua code apparently

  • All
  • Subscribed
  • Moderated
  • Favorites
  • programming@programming.dev
  • DreamBathrooms
  • everett
  • osvaldo12
  • magazineikmin
  • thenastyranch
  • rosin
  • normalnudes
  • Youngstown
  • Durango
  • slotface
  • ngwrru68w68
  • kavyap
  • mdbf
  • InstantRegret
  • JUstTest
  • ethstaker
  • GTA5RPClips
  • tacticalgear
  • Leos
  • anitta
  • modclub
  • khanakhh
  • cubers
  • cisconetworking
  • megavids
  • provamag3
  • tester
  • lostlight
  • All magazines