Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've a simple JSON look like:

{
  "account_login" : "[email protected]",
  "view_id" : 1868715,
  "join_id" : "utm_campaign=toyota&utm_content=multiformat_sites&utm_medium=cpc&utm_source=facebook",
  "start_date" : "2020-02-03",
  "end_date" : "2020-08-30"
}

With following Groovy script (from this answer):

def content = """
{
  "account_login" : "[email protected]",
  "view_id" : 1868715,
  "join_id" : "utm_campaign=toyota&utm_content=multiformat_sites&utm_medium=cpc&utm_source=facebook",
  "start_date" : "2020-02-03",
  "end_date" : "2020-08-30"
}
"""

def slurped = new JsonSlurper().parseText(content)
 
def builder = new JsonBuilder(slurped)

builder.content.join_id = builder.content.join_id.split("\s*&\s*")  //# to array
        .collectEntries{ 
                //# convert each item to map entry
                String[] utmMarks = it.trim().split("\s*=\s*")
                utmMarks[0] = [
                    "utm_medium"   : "ga:medium",
                    "utm_campaign" : "ga:campaign",
                    "utm_source"   : "ga:source",
                    "utm_content"  : "ga:adContent",
                    "utm_term"     : "ga:keyword",
                ].get( utmMarks[0] )
                utmMarks
            }
        .findAll{ 
             k,v-> k && v!=null  //# filter out empty/null keys
        } 
//builder.content.filters = ...
println(builder.toPrettyString())

I'll get:

{
    "account_login": "[email protected]",
    "view_id": 1868715,
    "join_id": {
        "ga:campaign": "toyota",
        "ga:adContent": "multiformat_sites",
        "ga:medium": "cpc",
        "ga:source": "facebook"
    },
    "start_date": "2020-02-03",
    "end_date": "2020-08-30"
}

I want to update this script (or write new) and add new property: array filters to modified json above. Expected output:

{
   "account_login":"[email protected]",
   "view_id":1868715,
   "join_id":{
      "ga:campaign":"toyota",
      "ga:adContent":"multiformat_sites",
      "ga:medium":"cpc",
      "ga:source":"facebook"
   },
   "start_date":"2020-02-03",
   "end_date":"2020-08-30",
   "converted_utm_marks":"ga:campaign=toyota&ga:adContent=multiformat_sites&ga:medium=cpc&ga:source=facebook",
   "filters":[
      {
         "dimensionName":"ga:medium",
         "operator":"EXACT",
         "expressions":[
            "cpc"
         ]
      },
      {
         "dimensionName":"ga:adContent",
         "operator":"EXACT",
         "expressions":[
            "multiformat_sites"
         ]
      },
      {
         "dimensionName":"ga:campaign",
         "operator":"EXACT",
         "expressions":[
            "toyota"
         ]
      },
      {
         "dimensionName":"ga:source",
         "operator":"EXACT",
         "expressions":[
            "facebook"
         ]
      }
   ]
}

But the problem is that the set of filters for each JSON will be different. This set depends directly on the join_id set. If JSON join_id will contain:

"join_id": {
        "ga:campaign": "toyota",
        "ga:keyword": "car"
    }

filters array should be:

[
      {
         "dimensionName":"ga:campaign",
         "operator":"EXACT",
         "expressions":[
            "toyota"
         ]
      },
      {
         "dimensionName":"ga:keyword",
         "operator":"EXACT",
         "expressions":[
            "car"
         ]
      }
]

operator is always equals EXACT. Property dimensionName - is a join_id.propety name. Expressions is a join_id.property value. So, property filters based on join_id and I need to loop through join_id property and build filters array with described structure. How to achieve expected output? JOLT configuration appreciated also.

I can't even simple iterate through join_id map:

slurped.join_id.each { println "Key: $it.key = Value: $it.value" }

I got the error:

/home/jdoodle.groovy: 24: illegal colon after argument expression;
   solution: a complex label expression before a colon must be parenthesized @ line 24, column 28.
             .collect { [it.ga:campaign] }  

UPDATE

I found out how to build this array:

def array = 
[ 
    filters: slurped.join_id.collect {key, value ->
            [
                dimensionName: key,
                operator: "EXACT",
                expressions: [
                    value
                    ]
            ]
        } 
]

Seems like i got it:

def slurped = new JsonSlurper().parseText(content)
def builder = new JsonBuilder(slurped)

builder.content.filters = builder.content.join_id.collect {key, value ->
            [
                dimensionName: key,
                operator: "EXACT",
                expressions: [
                    value
                    ]
            ]
        } 

Are there any better solutions?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.9k views
Welcome To Ask or Share your Answers For Others

1 Answer

def slurped = new JsonSlurper().parseText(content)
def builder = new JsonBuilder(slurped)

builder.content.filters = builder.content.join_id.collect {key, value ->
            [
                dimensionName: key,
                operator: "EXACT",
                expressions: [
                    value
                    ]
            ]
        } 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...