Dan
Dan

Reputation: 3268

Reading JSON file with Liquidsoap

I'd like to create a number of Liquidsoap sources with the same script but with a few variables e.g. station name.

I've seen that I can include a config.liq, for example, but it would be nice if I could read my configuration parameters from something not tied to the language, such as JSON. Liquidsoap has an of_json function but I'm unclear how I can read from a JSON file rather than a JSON string.

What can I do to have an configuration file that is not tied to the scripting language?

Upvotes: 4

Views: 1093

Answers (3)

Jonathan Stowe
Jonathan Stowe

Reputation: 21

I'm not sure when the facility was added to Liquidsoap (I have 1.2.1 here,) but assuming you have foo.json such as:

[
    {
         station_name : "Some Station",
         station_url  : "http://example.com/"
    },
    {
         station_name : "Some Other Station",
         station_url  : "http://other.example.com/"
    }
]

Then you should be able to read and parse it with something like:

m = file.contents("config.json")
print(of_json(default=[[("error", "badfile")]], m));

Giving

[[("station_name","Some Station"), ("station_url","http://example.com/")], [("station_name","Some Other Station"), ("station_url","http://other.example.com/")]]

If your JSON is a different format you would need to adjust the default template to the of_json

Hope that helps.

Upvotes: 2

freedrull
freedrull

Reputation: 2254

I can't find any commands to read a file directly from liquidsoap either.

You could always use environment variables:

station_name = getenv("STATION_NAME")

Upvotes: 0

Brad
Brad

Reputation: 163458

Good question. I don't have exact code for you, but I've actually been able to read from HTTP by shelling out to cURL. (And then, I found the built-in functions for HTTP requests, but those don't apply to your case I don't think.)

If you can't find a command to read a file directly, just use system() with a call to cat to dump the file.

Upvotes: 1

Related Questions