display content-type(Strategy) fields value in the form of array in groovy

184 views Asked by At

I have a snippets of groovy code (inside strategy.groovy file) as shown below in which I want to display the output in the form of desired array.

The content-type Strategy (in crafter) has the following fields:

  1. Title English (title_en_t)

  2. Title French (title_fr_t)

  3. Description English (description_en_t)

  4. Description French (description_fr_t)

  5. Players (players_o)

groovy code (inside strategy.groovy file):

private createStrategyElement(rootStrategy, ctx) {

    Element strategy = new DefaultElement("strategy")

    strategy.addElement("players_o.first_name_s").text = rootStrategy.players_o.item.component.first_name_s  != null ? rootStrategy.players_o.item.component.first_name_s : "" //Line A
    strategy.addElement("players_o.last_name_s").text = rootStrategy.players_o.item.component.last_name_s  != null ? rootStrategy.players_o.item.component.last_name_s : "" //Line B
    
   return strategy
}

Line A and Line B display the o/p in the following fashion:

current o/p:

players_o.first_name_s: "[John, The]"
players_o.last_name_s: "[Cena, Undertaker]"

PROBLEM STATEMENT:

I am wondering what changes I need to do in the groovy code above at Line A and Line B so that it displays the o/p in this fashion:

DESIRED O/P:

players: [{
    "item": [
        {
            "first_name_s": "John",
            "last_name_s": "Cena"
        },
        {
            "first_name_s": "The",
            "last_name_s": "Undertaker"
        }
    ]
}]
1

There are 1 answers

0
daggett On

don't know what is the crafter-cms. and you have not provided the input for your code.

i found DefaultElement and addElement in dom4j library - so i assume it's the one used in crafter-cms

https://dom4j.github.io/javadoc/2.0.1/org/dom4j/tree/DefaultElement.html

you are expecting json in output but dom4j used to work with xml...


however. just a try

private createStrategyElement(rootStrategy, ctx) {
  Element strategy = new DefaultElement("strategy")
  def players = strategy.addElement('players')
  for(i in rootStrategy.players_o.item.component){
    def item = players.addElement('item')
    item.addElement('first_name_s').text = i.first_name_s
    item.addElement('last_name_s').text = i.last_name_s
  }
  return strategy
}

the following code i used to debug it in standard groovy console:

@Grab(group='org.dom4j', module='dom4j', version='2.1.3')
import org.dom4j.tree.DefaultElement
import org.dom4j.Element
import org.dom4j.io.XMLWriter
import org.dom4j.io.OutputFormat

def rootStrategy=[
  players_o:[
    item:[
      component:[
        [
          first_name_s:'John',
          last_name_s: 'Cena',
        ],
        [
          first_name_s:'The',
          last_name_s: 'Undertaker'
        ],
      ]
    ]
  ]
]

private createStrategyElement(rootStrategy, ctx) {
  Element strategy = new DefaultElement("strategy")
  def players = strategy.addElement('players')
  for(i in rootStrategy.players_o.item.component){
    def item = players.addElement('item')
    item.addElement('first_name_s').text = i.first_name_s
    item.addElement('last_name_s').text = i.last_name_s
  }
  return strategy
}

println new StringWriter().with{w-> 
  new XMLWriter(w, OutputFormat.createPrettyPrint()).write( createStrategyElement(rootStrategy,null) )
  w.toString()
}