修改groovy中的REST响应

时间:2020-09-17 11:28:00

标签: json rest groovy

这是我的第一篇文章,所以我希望这是提出这些问题的合适地点。 我当前正在配置REST API调用。 API调用的结果由允许以常规方式配置API调用的应用程序使用。应用程序期望结果为JSONObject

我正在呼叫以下网址:

https://www.ebi.ac.uk/ols/api/select?ontology=cl&fieldList=iri,label&q=ba&rows=1

具有以下代码:

        def rest = new grails.plugins.rest.client.RestBuilder()
        def offset = (currentPage-1)*maxRows
        def url = 'https://www.ebi.ac.uk/ols/api/select?ontology=cl&fieldList=iri,label&q=ba&rows=1'
        def resp = rest.get(url)
        results = resp.json.response.docs

API响应如下:

{
   "responseHeader":{
      "status":0,
      "QTime":0,
      "params":{
         "hl":"true",
         "fl":"iri,label",
         "start":"0",
         "fq":[
            "ontology_name: (cl)",
            "is_obsolete:false"
         ],
         "rows":"1",
         "hl.simple.pre":"<b>",
         "bq":"type:ontology^10.0 is_defining_ontology:true^100.0 label_s:\"ba\"^1000  label_autosuggest_e:\"ba\"^500 synonym_s:\"ba\" synonym_autosuggest_e:\"ba\"^100",
         "q":"ba",
         "defType":"edismax",
         "hl.simple.post":"</b>",
         "qf":"label synonym label_autosuggest_e label_autosuggest synonym_autosuggest_e synonym_autosuggest shortform_autosuggest iri",
         "hl.fl":[
            "label_autosuggest",
            "label",
            "synonym_autosuggest",
            "synonym"
         ],
         "wt":"json"
      }
   },
   "response":{
      "numFound":146,
      "start":0,
      "docs":[
         {
            "iri":"http://purl.obolibrary.org/obo/CL_1000349",
            "label":"basal cell of epithelium of bronchus"
         }
      ]
   },
   "highlighting":{
      "cl:class:http://purl.obolibrary.org/obo/CL_1000349":{
         "label_autosuggest":[
            "<b>basal</b> cell of epithelium of bronchus"
         ],
         "synonym_autosuggest":[
            "<b>basal</b> cell of bronchus"
         ]
      }
   }
}

结果在下拉菜单中显示给用户。由于仅检索其中一个字段(例如,response.docs.iri)无法为用户提供足够的信息,因此我想创建一个新字段)或覆盖现有字段,以类似以下内容结束:

response.docs.iri_new = response.docs.iri + '(' + response.docs.label + ')'

在给定的示例中,这将导致类似http://purl.obolibrary.org/obo/CL_1000349(支气管上皮的基底细胞)

有可能吗?

1 个答案:

答案 0 :(得分:0)

欢迎

虽然您当然可以使用Groovy修改JSON,但这听起来更像是一个演示文稿问题-负责presented to users in a dropdown menu的应用程序可能更适合提取/组合/格式化API响应中的字段,以便对最终用户最有用。

话虽如此,each循环可以将组合字段添加到docs数组中的所有元素:

...
def results = resp.json.response.docs
def updated = results.docs.each{doc -> doc['iri_new'] = "${doc['iri']} (${doc['label']})"}

完整日志

groovy> import groovy.json.JsonSlurper 
groovy> import groovy.json.JsonOutput 
groovy> apiresponse=''' 
groovy> { 
groovy>    "response":{ 
groovy>       "numFound":146, 
groovy>       "start":0, 
groovy>       "docs":[ 
groovy>          { 
groovy>             "iri":"http://purl.obolibrary.org/obo/CL_1000349", 
groovy>             "label":"basal cell of epithelium of bronchus" 
groovy>          }, 
groovy>          { 
groovy>             "iri":"another uri", 
groovy>             "label":"a good label" 
groovy>          } 
groovy>       ] 
groovy>    } 
groovy> } 
groovy> ''' 
groovy> slurped = new JsonSlurper().parseText(apiresponse) 
groovy> println "Before:" 
groovy> println JsonOutput.prettyPrint(JsonOutput.toJson(slurped.response.docs)) 
groovy> updated = slurped.response.docs.each{doc -> doc['iri_new'] = "${doc['iri']} (${doc['label']})"} 
groovy> println "After:" 
groovy> println JsonOutput.prettyPrint(JsonOutput.toJson(updated)) 
 
Before:
[
    {
        "iri": "http://purl.obolibrary.org/obo/CL_1000349",
        "label": "basal cell of epithelium of bronchus"
    },
    {
        "iri": "another uri",
        "label": "a good label"
    }
]
After:
[
    {
        "iri": "http://purl.obolibrary.org/obo/CL_1000349",
        "label": "basal cell of epithelium of bronchus",
        "iri_new": "http://purl.obolibrary.org/obo/CL_1000349 (basal cell of epithelium of bronchus)"
    },
    {
        "iri": "another uri",
        "label": "a good label",
        "iri_new": "another uri (a good label)"
    }
]
相关问题