# Hi folks. Does anybody know how to parse/convert a String to a Distribution? For e

**URL:** <https://discourse.julialang.org/t/hi-folks-does-anybody-know-how-to-parse-convert-a-string-to-a-distribution-for-e/56257>\
**Category:** General Usage\
**Created:** [March 1, 2021, 3:31pm UTC](https://discourse.julialang.org/t/hi-folks-does-anybody-know-how-to-parse-convert-a-string-to-a-distribution-for-e/56257 "2021-03-01T15:31:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [March 1, 2021, 3:31pm UTC](https://discourse.julialang.org/t/hi-folks-does-anybody-know-how-to-parse-convert-a-string-to-a-distribution-for-e/56257/1 "2021-03-01T15:31:16Z")

</div>

Hi folks. Does anybody know how to parse/convert a String to a Distribution? For example “Beta(2,5)” -\> Beta{Float64}(α=2.0, β=5.0).  
I am trying to store probability distribution values in JSON format and read them into Julia, but am unable to successfully parse/convert.

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1614612633126800?thread_ts=1614612633.126800&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 1, 2021, 3:43pm UTC](https://discourse.julialang.org/t/hi-folks-does-anybody-know-how-to-parse-convert-a-string-to-a-distribution-for-e/56257/2 "2021-03-01T15:43:33Z")

</div>

Terribly unsafe, but `eval(Meta.parse("Beta(2,5)"))`. Safer, and far more work, you could split the string on punctuation, verify the substrings, and lookup the distribution based on the values. It depends on how much you trust the input. There’s some middle ground where you Meta.parse the string and validate the resultant expression before eval’ing it:

```julia
e=Meta.parse(s); if e.head==:call && e.args[1] in names(Distributions) && all(a->isa(a,Number),e.args[2:end]); eval(e); end

```

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [March 1, 2021, 4:33pm UTC](https://discourse.julialang.org/t/hi-folks-does-anybody-know-how-to-parse-convert-a-string-to-a-distribution-for-e/56257/3 "2021-03-01T16:33:05Z")

</div>

Maybe use `JSON3` and `StructTypes`. Define your own `struct` with a field distribution. That is what I did with `FixedPointDecimals`.  
[https://juliadata.github.io/StructTypes.jl/stable/#StructTypes.StringType](https://juliadata.github.io/StructTypes.jl/stable/#StructTypes.StringType)
