4
votes

I have case class with field AnyContent. I get it from DB as

AnyContentAsText( //some value)

Than when I get it in JSON like text

Json.obj("body"->content.asText)

it returns

[{"body":"AnyContentAsJson({\"ma\":\"[email protected]\"})"}]

When I want get it like JSON

Json.obj(content.asJson)

I get

[null]

How can I get it like JSON but not null of course?

2
It's not clear to me what your case class looks like and what the asText method is doing - mfirry

2 Answers

1
votes

The only way to go from AnyContentAsText to JSON would be to simply do Json.parse(content.asText).

However, it's odd that you're getting a value from your DB as AnyContentAsText. AnyContentAsText and all other subclasses of AnyContent are really intended for the request lifecycle. When you consume a request in a controller method, the first thing you should be doing is parsing your AnyContent into the expected underlying value (text, json, etc.) and then do any business logic/persistence with those underlying values.

1
votes

If you're receiving AnyContentAsText on the backend, check the request headers that the client sends. I had forgotten the "Content-Type": "application/json" header on my POST with JSON content. After adding the header, I received AnyContentAsJson on the backend side, as expected.