[VegaLite] How to convert a string to VL spec?

I have a string variable specifying a VL plot. How can I use the string variable with @vl_str in order to plot a graph? e.g.
julia> s
“{”$schema":"[https://vega.github.io/schema/vega-lite/v5.json](https://vega.github.io/schema/vega-lite/v5.json/)",“encoding”:{“x”:{“field”:“a”,“type”:“ordinal”},“y”:{“field”:“b”,“type”:“quantitative”}},“data”:{“values”:[{“b”:28,“a”:“A”},{“b”:55,“a”:“B”},{“b”:43,“a”:“C”},{“b”:91,“a”:“D”},{“b”:81,“a”:“E”},{“b”:53,“a”:“F”},{“b”:19,“a”:“G”},{“b”:87,“a”:“H”},{“b”:52,“a”:“I”}]},“mark”:“bar”,“description”:“A simple bar chart with embedded data.”}"

julia> @vl_str s
ERROR: LoadError: MethodError: no method matching parse(::Symbol)
Closest candidates are:
parse(::AbstractString; dicttype, inttype, allownan, null) at /home/peter/.julia/packages/JSON/3rsiS/src/Parser.jl:453
parse(::IO; dicttype, inttype, allownan, null) at /home/peter/.julia/packages/JSON/3rsiS/src/Parser.jl:484
Stacktrace:
[1] @vl_str(::LineNumberNode, ::Module, ::Any) at /home/peter/.julia/packages/VegaLite/6J0HU/src/dsl_str_macro/dsl_str_macro.jl:2
in expression starting at REPL[29]:1

That doesn’t look like a valid spec. Could you reformat your example in a code block without the (escaped) markdown formatting?

My question is about how to create VLSpec from a normal string variable. With @vl_str macro, I got the error. I have to replace the variable with its string value before applying @vl_str, I dont know how. Would you help me?

I see, so you can’t specify the plot spec inline? In that case, adding the following works:

using VegaLite: VLSpec
using JSON

VLSpec(JSON.parse(s))

This is exactly what VegaLite does when you use @vl_str, but skips the macro escaping that makes using @vl_str with a non-literal fail (i.e. your error above). @vl_str and vl"..." are meant for working with literals like so.

Great. Exactly what I want to know. How about adding the tip in VegaLite.jl document?

I’m sure they wouldn’t mind a PR adding to https://github.com/queryverse/VegaLite.jl/blob/master/docs/src/userguide/vlspec.md :slight_smile: