0

I am new in groovy and I want to construct a json object with the builder

{
    "query": {
        "bool": {
            "must": [
                { 
                    "bool": {
                        "should": [
                            { "match": { "content": "scontent" } },
                            { "match": { "title":"stitle" } }
                        ]
                    }
                },
                {
                    "bool": {
                        "should": [
                            { "match": { "a1": "v1" } },
                            { "match": { "a2":"v2" } },
                            ... and so on ...
                            { "match": { "an":"vn" } }
                        ]
                    }
                }
            ]
        }
    },
    "highlight": {
        "fields": {
            "content":{}
        }
    }
}

I search a lot of on other posts on stackoverflow and I write this code So I did this but no way to get what I want :

JsonBuilder builder = new JsonBuilder()
def body = builder {
    from Lib.or(qQuery.start, 0)
    size Lib.or(qQuery.num, 10)
    query {
        bool {
            must [
                    {
                        bool {
                            should [
                                    { match { content 'scontent' } },
                                    { match { title 'stitle' } }
                            ]
                        }
                    },
                    {
                        bool {
                            should myVals.collect {[
                                    'match' : { it.key it.value }

                            ]}
                        }
                    }
            ]
        }
    }
    highlight {
        fields {
            content {}
        }
    }
}

Thanks for any help !

1 Answer 1

3

I think you can make this work with the JsonBuilder as is, but it is usually easier to create the data structure using maps and lists (which is what the builder outputs) in groovy as you have more control there.

Example code:

import groovy.json.* 

def data = [
  query: [
    bool: [
      must: [
        [bool: 
          [should: [
            [match: [ content: 'scontent']],
            [match: [ title:   'stitle']]
          ]]
        ],
        [bool: 
          [should: [
            [match: [ a1: 'v1']],
            [match: [ a2: 'v2']], 
            [match: [ vn: 'vn']]
          ]]
        ]
      ]
    ]
  ]
]

println JsonOutput.prettyPrint(JsonOutput.toJson(data))

produces:

{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "match": {
                                    "content": "scontent"
                                }
                            },
                            {
                                "match": {
                                    "title": "stitle"
                                }
                            }
                        ]
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "match": {
                                    "a1": "v1"
                                }
                            },
                            {
                                "match": {
                                    "a2": "v2"
                                }
                            },
                            {
                                "match": {
                                    "vn": "vn"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

I did not include your full json as it takes up some space, but the structure is there. Note the use of lists ([valueA, valueB]) vs maps ([someKey: someValue]) in the data structure.

Granted this makes the JsonBuilder less than 100% useful but I haven't seen any concise ways of including lists of large json objects in a list within the structure. You can do:

def json = JsonBuilder()

json.query { 
    bool('list', 'of', 'values')
}

but for larger structures as list elements I would say go with the lists and maps approach.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your response /why this def json = new JsonBuilder() is defined ??
if you are referring to the first code block, then that was a mistake by me. Fixed it now.
Happy to help. If this answer or any other one solved your issue, please mark it as accepted by clicking the green check mark. This helps keep the focus on older SO which still don't have answers. Or if you are hoping to get a better answer, don't click the green check mark and somebody might come along and provide you with one : )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.