Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Jolt Specification to complex structure

avatar
New Contributor

Hi,

I've got an INPUT-json that looks like this:

{
"vendor" : "myINC",
"beginTime" : "2020-01-21T10:00:00+08:00",
"userLabel" : "DB",
"Types" : [ {
"Name" : "type001",
"subTypes" : [ {
"subtype003" : {
"counter1" : "0",
"counter5" : "0",
"counter54" : "0"
}
},{
"subtype001": {
"counter002": "0",
"counter5": "0",
"counter65": "0"
}
} ]
}, {
"Name" : "type002",
"subTypes" : [ {
"subtype006" : {
"counter1" : "0",
"counter5" : "0",
"counter54" : "0"
}},{
"subtype009" : {
"counter002": "0",
"counter5": "0",
"counter65": "0"
}
}]
} ]
}

 

and would need a OUTPUT-json like this:

 

{
"vendor" : "myINC",
"beginTime" : "2020-01-21T10:00:00+08:00",
"userLabel" : "DG",
"Types" : [ {
"Name" : "type001",
"subType" : [ {
"Name": "subtype003",
"Counters": [
{
"name": "counter1",
"value": "0"
},{
"name": "counter5",
"value": "0"
},{
"name": "counter54",
"value": "0"
} ]
},{
"Name": "subtype001",
"Counters": [
{
"name": "counter002",
"value": "0"
},{
"name": "counter5",
"value": "0"
},{
"name": "counter65",
"value": "0"
}]
} ]
}, {
"Name" : "type002",
"subType" : [ {
"Name": "subtype006",
"Counters": [
{
"name": "counter1",
"value": "0"
},{
"name": "counter5",
"value": "0"
},{
"name": "counter54",
"value": "0"
} ]
},{
"Name": "subtype009",
"Counters": [
{
"name": "counter002",
"value": "0"
},{
"name": "counter5",
"value": "0"
},{
"name": "counter65",
"value": "0"
}]
} ]
} ]
}

 

Couldn't make any real breakthrough if not getting till here, some help really appreciated.

Thanks 

1 ACCEPTED SOLUTION

avatar
Expert Contributor

@dg 
Your desired output contains DG as value for userLabel. I am not sure if it is a typo, so, I tried converting it as well. If not needed, you can remove the modify-overwrite-beta part.

[
{
"operation": "modify-overwrite-beta",
"spec": {
"userLabel": "DG"
}
},
{
"operation": "shift",
"spec": {
"Types": {
"*": {
"subTypes": {
"*": {
"*": {
"$0": "Types[&4].subType[&2].Name",
"*": {
"$": "Types[&5].subType[&3].CounterNames[]",
"@": "Types[&5].subType[&3].CounterValues[]"
}
}
}
},
"*": "Types[&1].&"
}
},
"*": "&"
}
},
{
"operation": "shift",
"spec": {
"Types": {
"*": {
"subType": {
"*": {
"CounterNames": {
"*": "Types[&4].subType[&2].Counters[&].name"
},
"CounterValues": {
"*": "Types[&4].subType[&2].Counters[&].value"
},
"*": "Types[&3].subType[&1].&"
}
},
"*": "Types[&1].&"
}
},
"*": "&"
}
}
]

Here, the second spec(first shift) gives the following:

{
  "vendor" : "myINC",
  "beginTime" : "2020-01-21T10:00:00+08:00",
  "userLabel" : "DG",
  "Types" : [ {
    "Name" : "type001",
    "subType" : [ {
      "Name" : "subtype003",
      "CounterNames" : [ "counter1", "counter5", "counter54" ],
      "CounterValues" : [ "0", "0", "0" ]
    }, {
      "Name" : "subtype001",
      "CounterNames" : [ "counter002", "counter5", "counter65" ],
      "CounterValues" : [ "0", "0", "0" ]
    } ]
  }, {
    "Name" : "type002",
    "subType" : [ {
      "Name" : "subtype006",
      "CounterNames" : [ "counter1", "counter5", "counter54" ],
      "CounterValues" : [ "0", "0", "0" ]
    }, {
      "Name" : "subtype009",
      "CounterNames" : [ "counter002", "counter5", "counter65" ],
      "CounterValues" : [ "0", "0", "0" ]
    } ]
  } ]
}

The last spec gives you the desired output

View solution in original post

2 REPLIES 2

avatar
Expert Contributor

@dg 
Your desired output contains DG as value for userLabel. I am not sure if it is a typo, so, I tried converting it as well. If not needed, you can remove the modify-overwrite-beta part.

[
{
"operation": "modify-overwrite-beta",
"spec": {
"userLabel": "DG"
}
},
{
"operation": "shift",
"spec": {
"Types": {
"*": {
"subTypes": {
"*": {
"*": {
"$0": "Types[&4].subType[&2].Name",
"*": {
"$": "Types[&5].subType[&3].CounterNames[]",
"@": "Types[&5].subType[&3].CounterValues[]"
}
}
}
},
"*": "Types[&1].&"
}
},
"*": "&"
}
},
{
"operation": "shift",
"spec": {
"Types": {
"*": {
"subType": {
"*": {
"CounterNames": {
"*": "Types[&4].subType[&2].Counters[&].name"
},
"CounterValues": {
"*": "Types[&4].subType[&2].Counters[&].value"
},
"*": "Types[&3].subType[&1].&"
}
},
"*": "Types[&1].&"
}
},
"*": "&"
}
}
]

Here, the second spec(first shift) gives the following:

{
  "vendor" : "myINC",
  "beginTime" : "2020-01-21T10:00:00+08:00",
  "userLabel" : "DG",
  "Types" : [ {
    "Name" : "type001",
    "subType" : [ {
      "Name" : "subtype003",
      "CounterNames" : [ "counter1", "counter5", "counter54" ],
      "CounterValues" : [ "0", "0", "0" ]
    }, {
      "Name" : "subtype001",
      "CounterNames" : [ "counter002", "counter5", "counter65" ],
      "CounterValues" : [ "0", "0", "0" ]
    } ]
  }, {
    "Name" : "type002",
    "subType" : [ {
      "Name" : "subtype006",
      "CounterNames" : [ "counter1", "counter5", "counter54" ],
      "CounterValues" : [ "0", "0", "0" ]
    }, {
      "Name" : "subtype009",
      "CounterNames" : [ "counter002", "counter5", "counter65" ],
      "CounterValues" : [ "0", "0", "0" ]
    } ]
  } ]
}

The last spec gives you the desired output

avatar
New Contributor

Hi Pvvk,

I really want to thank You!

Your Jolt is great.

I really need to work on my jolt skills and this helps me, have to exercise my reading of specs.

 

Thanks

Davide