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

**URL:** <https://discourse.julialang.org/t/vegalite-how-to-convert-a-string-to-vl-spec/46767>\
**Category:** General Usage\
**Created:** [September 17, 2020, 10:28am UTC](https://discourse.julialang.org/t/vegalite-how-to-convert-a-string-to-vl-spec/46767 "2020-09-17T10:28:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![peters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peters/32/5924_2.png) [@peters](https://discourse.julialang.org/u/peters)\
**Post date:** [September 17, 2020, 10:28am UTC](https://discourse.julialang.org/t/vegalite-how-to-convert-a-string-to-vl-spec/46767/1 "2020-09-17T10:28:03Z")

</div>

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%5C)]([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

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [September 17, 2020, 2:39pm UTC](https://discourse.julialang.org/t/vegalite-how-to-convert-a-string-to-vl-spec/46767/2 "2020-09-17T14:39:44Z")

</div>

> [@peters](#):
>
> ```julia
> [https://vega.github.io/schema/vega-lite/v5.json](https://vega.github.io/schema/vega-lite/v5.json/)
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![peters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peters/32/5924_2.png) [@peters](https://discourse.julialang.org/u/peters)\
**Post date:** [September 17, 2020, 2:58pm UTC](https://discourse.julialang.org/t/vegalite-how-to-convert-a-string-to-vl-spec/46767/3 "2020-09-17T14:58:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [September 17, 2020, 3:51pm UTC](https://discourse.julialang.org/t/vegalite-how-to-convert-a-string-to-vl-spec/46767/4 "2020-09-17T15:51:59Z")

</div>

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

```julia
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](https://www.queryverse.org/VegaLite.jl/stable/userguide/vlspec/#The-vl-string-macro-1).

---

<div class="post-metadata">

**Author:** ![peters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peters/32/5924_2.png) [@peters](https://discourse.julialang.org/u/peters)\
**Post date:** [September 17, 2020, 4:42pm UTC](https://discourse.julialang.org/t/vegalite-how-to-convert-a-string-to-vl-spec/46767/5 "2020-09-17T16:42:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [September 17, 2020, 5:34pm UTC](https://discourse.julialang.org/t/vegalite-how-to-convert-a-string-to-vl-spec/46767/6 "2020-09-17T17:34:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![peters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peters/32/5924_2.png) [@peters](https://discourse.julialang.org/u/peters)\
**Post date:** [September 19, 2020, 7:45am UTC](https://discourse.julialang.org/t/vegalite-how-to-convert-a-string-to-vl-spec/46767/7 "2020-09-19T07:45:15Z")

</div>

[https://github.com/queryverse/VegaLite.jl/issues/334](https://github.com/queryverse/VegaLite.jl/issues/334)
