scala parse json objects in order










1















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?










share|improve this question
























  • 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















1















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?










share|improve this question
























  • 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













1












1








1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























  • Wow. That's cool. Thanks a lot.

    – yalkris
    Nov 13 '18 at 22:41











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
);



);













draft saved

draft discarded


















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









1














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.






share|improve this answer























  • Wow. That's cool. Thanks a lot.

    – yalkris
    Nov 13 '18 at 22:41















1














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.






share|improve this answer























  • Wow. That's cool. Thanks a lot.

    – yalkris
    Nov 13 '18 at 22:41













1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)