scala parse json objects in order
I have this following json input where I am trying to parse the name field in-order
scala> result
res6: play.api.libs.json.JsValue = "L0":
"name":"FASHION","id":"50000","L1":"name":"ACCESSORIES AND TRAVEL","id":"51000","L2":"name":"FASHION ACCESSORIES","id":"51001","L3":"name":"MENS FASHION ACCESSORIES","id":"51100","L4":"name":"MENS HATS","id":"51204"
scala> result \ "name"
res5: Seq[play.api.libs.json.JsValue] = List("ACCESSORIES AND TRAVEL", "MENS HATS", "MENS FASHION ACCESSORIES", "FASHION ACCESSORIES", "FASHION")
What I am trying is to get those names in-order like
List("FASHION", "ACCESSORIES AND TRAVEL", "FASHION ACCESSORIES", "MENS FASHION ACCESSORIES", "MENS HATS")
Is there a way to achieve that with play Json library?
scala play-json
add a comment |
I have this following json input where I am trying to parse the name field in-order
scala> result
res6: play.api.libs.json.JsValue = "L0":
"name":"FASHION","id":"50000","L1":"name":"ACCESSORIES AND TRAVEL","id":"51000","L2":"name":"FASHION ACCESSORIES","id":"51001","L3":"name":"MENS FASHION ACCESSORIES","id":"51100","L4":"name":"MENS HATS","id":"51204"
scala> result \ "name"
res5: Seq[play.api.libs.json.JsValue] = List("ACCESSORIES AND TRAVEL", "MENS HATS", "MENS FASHION ACCESSORIES", "FASHION ACCESSORIES", "FASHION")
What I am trying is to get those names in-order like
List("FASHION", "ACCESSORIES AND TRAVEL", "FASHION ACCESSORIES", "MENS FASHION ACCESSORIES", "MENS HATS")
Is there a way to achieve that with play Json library?
scala play-json
What have you tried by yourself?
– cchantep
Sep 20 '18 at 23:37
Yes. result \ "name" is what I have right now. I am trying to do it using a regex but I am wondering if there's an easy way with play Json library itself
– yalkris
Sep 21 '18 at 0:22
First have a look at the doc about how to parse/validate
– cchantep
Sep 21 '18 at 0:48
add a comment |
I have this following json input where I am trying to parse the name field in-order
scala> result
res6: play.api.libs.json.JsValue = "L0":
"name":"FASHION","id":"50000","L1":"name":"ACCESSORIES AND TRAVEL","id":"51000","L2":"name":"FASHION ACCESSORIES","id":"51001","L3":"name":"MENS FASHION ACCESSORIES","id":"51100","L4":"name":"MENS HATS","id":"51204"
scala> result \ "name"
res5: Seq[play.api.libs.json.JsValue] = List("ACCESSORIES AND TRAVEL", "MENS HATS", "MENS FASHION ACCESSORIES", "FASHION ACCESSORIES", "FASHION")
What I am trying is to get those names in-order like
List("FASHION", "ACCESSORIES AND TRAVEL", "FASHION ACCESSORIES", "MENS FASHION ACCESSORIES", "MENS HATS")
Is there a way to achieve that with play Json library?
scala play-json
I have this following json input where I am trying to parse the name field in-order
scala> result
res6: play.api.libs.json.JsValue = "L0":
"name":"FASHION","id":"50000","L1":"name":"ACCESSORIES AND TRAVEL","id":"51000","L2":"name":"FASHION ACCESSORIES","id":"51001","L3":"name":"MENS FASHION ACCESSORIES","id":"51100","L4":"name":"MENS HATS","id":"51204"
scala> result \ "name"
res5: Seq[play.api.libs.json.JsValue] = List("ACCESSORIES AND TRAVEL", "MENS HATS", "MENS FASHION ACCESSORIES", "FASHION ACCESSORIES", "FASHION")
What I am trying is to get those names in-order like
List("FASHION", "ACCESSORIES AND TRAVEL", "FASHION ACCESSORIES", "MENS FASHION ACCESSORIES", "MENS HATS")
Is there a way to achieve that with play Json library?
scala play-json
scala play-json
edited Sep 20 '18 at 22:52
yalkris
asked Sep 20 '18 at 22:35
yalkrisyalkris
1,20231842
1,20231842
What have you tried by yourself?
– cchantep
Sep 20 '18 at 23:37
Yes. result \ "name" is what I have right now. I am trying to do it using a regex but I am wondering if there's an easy way with play Json library itself
– yalkris
Sep 21 '18 at 0:22
First have a look at the doc about how to parse/validate
– cchantep
Sep 21 '18 at 0:48
add a comment |
What have you tried by yourself?
– cchantep
Sep 20 '18 at 23:37
Yes. result \ "name" is what I have right now. I am trying to do it using a regex but I am wondering if there's an easy way with play Json library itself
– yalkris
Sep 21 '18 at 0:22
First have a look at the doc about how to parse/validate
– cchantep
Sep 21 '18 at 0:48
What have you tried by yourself?
– cchantep
Sep 20 '18 at 23:37
What have you tried by yourself?
– cchantep
Sep 20 '18 at 23:37
Yes. result \ "name" is what I have right now. I am trying to do it using a regex but I am wondering if there's an easy way with play Json library itself
– yalkris
Sep 21 '18 at 0:22
Yes. result \ "name" is what I have right now. I am trying to do it using a regex but I am wondering if there's an easy way with play Json library itself
– yalkris
Sep 21 '18 at 0:22
First have a look at the doc about how to parse/validate
– cchantep
Sep 21 '18 at 0:48
First have a look at the doc about how to parse/validate
– cchantep
Sep 21 '18 at 0:48
add a comment |
1 Answer
1
active
oldest
votes
With Play JSON I always use case classes
. So your example would look like:
import play.api.libs.json._
val json = """"L0":
"name":"FASHION","id":"50000","L1":"name":"ACCESSORIES AND TRAVEL","id":"51000","L2":"name":"FASHION ACCESSORIES","id":"51001","L3":"name":"MENS FASHION ACCESSORIES","id":"51100","L4":"name":"MENS HATS","id":"51204"
"""
case class Element(id: String, name: String)
object Element
implicit val jsonFormat: Format[Element] = Json.format[Element]
Json.parse(json).validate[Map[String, Element]] match
case JsSuccess(elems, _) => println(elems.toList.sortBy(_._1).map(e => e._2.name))
case other => println(s"Handle exception $other")
What this gives you, is that you can sort the result by the key - the info that is lost in your solution.
Wow. That's cool. Thanks a lot.
– yalkris
Nov 13 '18 at 22:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52434254%2fscala-parse-json-objects-in-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
With Play JSON I always use case classes
. So your example would look like:
import play.api.libs.json._
val json = """"L0":
"name":"FASHION","id":"50000","L1":"name":"ACCESSORIES AND TRAVEL","id":"51000","L2":"name":"FASHION ACCESSORIES","id":"51001","L3":"name":"MENS FASHION ACCESSORIES","id":"51100","L4":"name":"MENS HATS","id":"51204"
"""
case class Element(id: String, name: String)
object Element
implicit val jsonFormat: Format[Element] = Json.format[Element]
Json.parse(json).validate[Map[String, Element]] match
case JsSuccess(elems, _) => println(elems.toList.sortBy(_._1).map(e => e._2.name))
case other => println(s"Handle exception $other")
What this gives you, is that you can sort the result by the key - the info that is lost in your solution.
Wow. That's cool. Thanks a lot.
– yalkris
Nov 13 '18 at 22:41
add a comment |
With Play JSON I always use case classes
. So your example would look like:
import play.api.libs.json._
val json = """"L0":
"name":"FASHION","id":"50000","L1":"name":"ACCESSORIES AND TRAVEL","id":"51000","L2":"name":"FASHION ACCESSORIES","id":"51001","L3":"name":"MENS FASHION ACCESSORIES","id":"51100","L4":"name":"MENS HATS","id":"51204"
"""
case class Element(id: String, name: String)
object Element
implicit val jsonFormat: Format[Element] = Json.format[Element]
Json.parse(json).validate[Map[String, Element]] match
case JsSuccess(elems, _) => println(elems.toList.sortBy(_._1).map(e => e._2.name))
case other => println(s"Handle exception $other")
What this gives you, is that you can sort the result by the key - the info that is lost in your solution.
Wow. That's cool. Thanks a lot.
– yalkris
Nov 13 '18 at 22:41
add a comment |
With Play JSON I always use case classes
. So your example would look like:
import play.api.libs.json._
val json = """"L0":
"name":"FASHION","id":"50000","L1":"name":"ACCESSORIES AND TRAVEL","id":"51000","L2":"name":"FASHION ACCESSORIES","id":"51001","L3":"name":"MENS FASHION ACCESSORIES","id":"51100","L4":"name":"MENS HATS","id":"51204"
"""
case class Element(id: String, name: String)
object Element
implicit val jsonFormat: Format[Element] = Json.format[Element]
Json.parse(json).validate[Map[String, Element]] match
case JsSuccess(elems, _) => println(elems.toList.sortBy(_._1).map(e => e._2.name))
case other => println(s"Handle exception $other")
What this gives you, is that you can sort the result by the key - the info that is lost in your solution.
With Play JSON I always use case classes
. So your example would look like:
import play.api.libs.json._
val json = """"L0":
"name":"FASHION","id":"50000","L1":"name":"ACCESSORIES AND TRAVEL","id":"51000","L2":"name":"FASHION ACCESSORIES","id":"51001","L3":"name":"MENS FASHION ACCESSORIES","id":"51100","L4":"name":"MENS HATS","id":"51204"
"""
case class Element(id: String, name: String)
object Element
implicit val jsonFormat: Format[Element] = Json.format[Element]
Json.parse(json).validate[Map[String, Element]] match
case JsSuccess(elems, _) => println(elems.toList.sortBy(_._1).map(e => e._2.name))
case other => println(s"Handle exception $other")
What this gives you, is that you can sort the result by the key - the info that is lost in your solution.
answered Nov 13 '18 at 15:57
pmepme
3,28211830
3,28211830
Wow. That's cool. Thanks a lot.
– yalkris
Nov 13 '18 at 22:41
add a comment |
Wow. That's cool. Thanks a lot.
– yalkris
Nov 13 '18 at 22:41
Wow. That's cool. Thanks a lot.
– yalkris
Nov 13 '18 at 22:41
Wow. That's cool. Thanks a lot.
– yalkris
Nov 13 '18 at 22:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52434254%2fscala-parse-json-objects-in-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What have you tried by yourself?
– cchantep
Sep 20 '18 at 23:37
Yes. result \ "name" is what I have right now. I am trying to do it using a regex but I am wondering if there's an easy way with play Json library itself
– yalkris
Sep 21 '18 at 0:22
First have a look at the doc about how to parse/validate
– cchantep
Sep 21 '18 at 0:48