JSON object to array with JOLT - json

I have the following JSON object
Input:
{
"17.39.108.85:80": [],
"10.204.32.9:443": [
{
"status": "DOWN"
}
]
}
and trying to transform it into a list/array as below :
Desired Output:
[
{
"17.39.108.85:80": []
},
{
"10.204.32.9:443": [
{
"status": "DOWN"
}
]
}
]
What's the best way to use Jolt.

You can use this spec:
Currently, your input is correct, but you need to put each key into an array.
You can get each key in the object and send it to an array with [#2].
Note: #2 is the index of each key. for example: 17.39.108.85:80 is 0 and 10.204.32.9:443 is 1.
[
{
"operation": "shift",
"spec": {
"*": {
"#": "[#2].&"
}
}
}
]

Related

Match JSON arrays with JOLT

I have JSON from REST API:
{
"fields": [
"advertiser_id",
"campaign_id",
"day"
],
"data": [
[
"8905",
"234870",
"2021-09-28"
],
[
"5634",
"88467870",
"2021-09-28"
]
]
}
I want to match values inside fields array with values inside data. The have same order. So I expect to get:
[
{
"advertiser_id": "8905",
"campaign_id": "234870",
"day": "2021-09-28"
},
{
"advertiser_id": "5634",
"campaign_id": "88467870",
"day": "2021-09-28"
}
]
Any ways to do it with JOLT?
You can use a shift transformation spec in which
go 4 levels up (traverse once:, and { triple) in order to reach
fields array as picking sub-arrays of data array by using [&1]
dissipate all returning key-value pairs through use of [&2]. node
such as
[
{
"operation": "shift",
"spec": {
"data": {
"*": {
"*": {
"#": "[&2].#(4,fields[&1])"
}
}
}
}
}
]
the demo on the site http://jolt-demo.appspot.com/ is

Jolt get value of key value pair where key equals host in array of key value pairs

I have a array of Key Value Pairs in my json object, and need to pull out a set value based on the key being equal to host.
{
"pairs" : [ {
"key" : "Host",
"value" : "site-a"
}, {
"key" : "User",
"value" : "user42"
}
}
I can't match based on position as it could be anywhere in the array of pairs, and the array can vary in size.
My Current Jolt spec looks like, but it's just listing each pair:
[
{
"operation": "shift",
"spec": {
"requestHeaderFields": {
"*": {
"value": "#(1,key)"
}
}
}
}
]
The current output is:
{
"Host" : "site-a",
"User-Agent" : "user42"
}
My desired output would be the following, noting the field name change:
{
"HostSite" : "site-a",
}
I am wondering if I first need to do a modify-overwrite-beta operation and then a shift?
This jolt will do the trick. The idea is to check when key has a Host value and then retrieve value:
[
{
"operation": "shift",
"spec": {
"pairs": {
"*": {
"key": {
"Host": {
"#(2,value)": "HostSite"
}
}
}
}
}
}
]

How can I combine two arrays to create a key value pair with Jolt?

I've already created a spec to convert my JSON input
{
"rows": [
{
"row": [
"row1",
"row2",
"row3"
],
"header": [
"header1",
"header2",
"header3"
]
},
{
"row": [
"row4",
"row5",
"row6"
],
"header": [
"header4",
"header5",
"header6"
]
}
]
}
to convert to key-value pairs as following object result :
{
"header1" : "row1",
"header2" : "row2",
"header3" : "row3",
"header4" : "row4",
"header5" : "row5",
"header6" : "row6"
}
Is this possible to do using Jolt?
Is there a copy/paste error in your input? Judging by your desired output, the second object's header array should be ["header4", "header5", "header6"]. If that's the case, this spec should work:
[
{
"operation": "shift",
"spec": {
"rows": {
"*": {
"header": {
"*": {
"*": {
"#(3,row[#2])": "&"
}
}
}
}
}
}
}
]
One option is to use the following shift transformation spec :
[
{
"operation": "shift",
"spec": {
"*s": { // rows array
"*": {
"&(1,1)": { // row array
"*": {
"#": "#(3,header[&1])"
}
}
}
}
}
}
]
where
"*s": { stands for rows
"&(1,1)": { -> not immediate(zeroth) level but one more level up by using &(1, and grab the value there the first asterisk exists by &(..,1)
"#": "#(3,header[&1])" -> 3 levels needed as stated at the right hand side traverse the colon
as well in order to reach the level of &(1,1) which is used to
represent the "row" array along with &1 representation to go one level up the tree to reach the indexes of the array "row" while matching with the values of "row" through use of # on the left hand side
the demo on the site http://jolt-demo.appspot.com/ is :

Jolt transform so that data of an element is the key and the value is another elements data

I need to perform a Jolt transformation on the below example json:
[ {
"name" : "foo",
"dataSample" : "red"
}, {
"name" : "bar",
"dataSample" : "amber"
}]
I need the output to look like:
{
"foo": "red",
"bar": "amber"
}
so far i've managed to extract the name value as the key, but i'm lost as to how to get the dataSample value as the value for the transformed element. Here's the Jolt script I have so far:
[
{
"operation" : "shift",
"spec" : {
"*" : {
"name" : {
"*" : "&"
}
}
}
}
]
You need to go back up the tree to get the value of the "name" field, rather than using the current value (&). This should work:
[
{
"operation": "shift",
"spec": {
"*": {
"name": {
"#(1,dataSample)": "#(2,name)"
}
}
}
}
]
[
{
"operation": "shift",
"spec": {
"*": {
"dataSample": "#(1,name)"
}
}
}
]

Jolt Transformation

I am trying to write a jolt transformation for below input -
{
"restaurantId": "ZZ4ORJDY3E",
"chainId": "a-b"
}
expected output is -
{
"ZZ4ORJDY3E" : {
"key" : "ZZ4ORJDY3E",
"start" : "a",
"end" : "b"
}
}
My spec is -
[
{
"operation": "shift",
"spec": {
"#restaurantId": "#restaurantId.key",
"chainId": {
"*-*": {
"$(0,1)": "#restaurantId.start",
"$(0,2)": "#restaurantId.end"
}
}
}
}
]
The spec is not transforming as expected output. i want learn how to use attributes inside string parser.
Spec
[
{
"operation": "shift",
"spec": {
"restaurantId": {
// match any value of restaurantId
"*": {
// write the value to of the key $ to the output
// where the output is the "value of the key".key
// kinda hokey
"$": "&.key"
}
},
"chainId": {
"*-*": {
// write each part of the chainId to the output
// at the value of restaurantId from back up the tree
"$(0,1)": "#(3,restaurantId).start",
"$(0,2)": "#(3,restaurantId).end"
}
}
}
}
]