jesus g_force Harris
jesus g_force Harris

Reputation: 516

How can I move a JsArray to the root with Json Transformers within a Json object?

How can I move a JsArray to the root with Json Transformers? Imagine a Json structure like so:

   {
     "key1" : "value1",
     "key2" : {
       "key21" : "catface",
       "key22" : true,
       "key23" : [
           {
              "key231": "alpha", 
              "key232": "beta", 
              "key232": "gamma"
           },
           ...
       ],
       "key24" : 234
    }

If I wanted to move key21 to the root then I would use - __.json.copyFrom((__ \ 'key2 \ 'key21).json.pick) but if I wanted to move key23 to the root; i.e.

   {
     "key23" : [
       {
          "key231": "alpha", 
          "key232": "beta", 
          "key232": "gamma"
       },
       ...
     ]
   }

but __.json.copyFrom((__ \ 'key2 \ 'key23).json.pick) causes the error:

A server errors occurred: when empty JsPath, expecting JsObject

So then I try __.json.copyFrom((__ \ 'key2 \ 'key23).json.pick[JsArray]) or __.json.copyFrom[JsArray]((__ \ 'key2 \ 'key23).json.pick[JsArray]) - but the same error

I couldn't find anything out there that addresses this specifically but I am hoping this is an easy one for someone. Many thanks

Upvotes: 0

Views: 41

Answers (1)

Saskia
Saskia

Reputation: 1056

the value you're picking needs a new key under root

(__ \ 'key23).json.copyFrom((__ \ 'key2 \ 'key23).json.pick)

Scastie

Upvotes: 1

Related Questions