I need to load a JSON
file with a list of cities in one of my controllers, in order to pass it to a view afterwards.
I have placed the file here: app/assets/jsons/countriesToCities.json
(By the way: is this an appropriate location, or should I place it somewhere else?)
I have read the docs and I can see it is possible to create a JsValue
from a string: https://www.playframework.com/documentation/2.4.x/ScalaJson#Using-string-parsing
I want to create a JsValue
in a similar fashion. The difference is that I want to load the content from a file, not from a string... I haven't found any code snippet on how to do this, unfortunately.
Do I have to use something else to read the file into a string and only then use the parse method on that string?
Code snippets with examples on how to do this in the answers will be highly appreciated! :)
Thank you very much in advance!
I usually need few small dictionaries to live in memory in order to build some plain LUTs (LookUp Tables). In order to achieve that I use the following piece of code (please it is worth noting I am using latest Play 2.6):
Using the previous function is just a matter of passing a
filename
path. Tailoring this to be used with Play you might need to place your file in a resource folder and to enable it you need to place this in yourbuild.sbt
file:Thus, you can use this resource folder this way in your Play class:
Here is how I managed to solve it:
Thanks for the help! :)
Looks like the comment about the possible duplicate is how to read a file from your app/assets folder. My answer is about how to parse Json from a stream. Combine the two and you should be good to go.
Json.parse
accepts a few different argument types, one of which isInputStream
.P.S. When you can't find what you're looking for in the written docs, the API Docs are a good place to start.
From Play 2.6, Environment has the
getExistingFile
,getFile
,resource
andresourceAsStream
methods, E.g.:(Note, in this case data.json is inside the conf folder)
https://www.playframework.com/documentation/2.6.x/api/scala/index.html#play.api.Environment