Dwarrior
Dwarrior

Reputation: 757

Jolt spec to parse and transform JSON array

This is my first time using Jolt and I am amazed at transformations it can perform. I followed the documentation and few posts online.

However, I am still have challenges with:

I have the following input

{
  "Body": [
    {
      "username": "some-user"
    },
    {
      "password": "*******"
    }
  ],
  "hostSource": "infos",
  "Host": [
    {
      "HOST_NAME": "xyz.com"
    },
    {
      "PORT": "9085"
    }
  ],
  "Headers": [
    {
      "Content-Type": "application/json"
    }
  ]
}

and my expected output is:

{
  "templateConfig": {
    "commonClientConfig ": {
      "item": [
        {
          "name": "Main API - BASICAUTH",
          "request": {
            "auth": {
              "type": "basic",
              "basic": [
                {
                  "key": "password",
                  "value": "*******",
                  "type": "string"
                },
                {
                  "key": "username",
                  "value": "some-user",
                  "type": "string"
                }
              ]
            },
            "method": "GET",
            "header": [
              {
                "key": "Content-Type",
                "value": "application/json",
                "type": "text"
              }
            ],
            "url": {
              "raw": "xyz.com/v9085/some-default-value"
            }
          }
        }
      ]
    }
  }
}

After referring few posts and documentation, I was able to get this far with the spec:

 [
  {
    "operation": "shift",
    "spec": {
      "Body": {
        "*": {
          "*": {
            "$": "commonClientConfig.item.request.auth.basic[&2].key",
            "@(1,&)": "commonClientConfig.item.request.auth.basic[&2].value"
          }
        }
      },
      "Headers": {
        "*": {
          "*": {
            "$": "commonClientConfig.item.request.header[&2].key",
            "@(1,&)": "commonClientConfig.item.request.header[&2].value"
          }
        }
      },
      "Host": {
        "*": {
          "*": {
            "@(1,&)": "commonClientConfig.item.request.url.raw"
          }
        }
      }
    }
    },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "content": "=join('/', @(2,commonClientConfig.item.request.url.raw))"
    }
  }    
]

I would really appreciate if someone can provide some guidance here with explanation.

Upvotes: 2

Views: 1803

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65228

You can use the following consecutive specs

[
  {
    "operation": "shift",
    "spec": {
      "#Main API - BASICAUTH": "name",
      "Body": {
        "#basic": "request.auth.type",
        "*": {
          "*": {
            "$": "request.auth.basic[&2].key",
            "@": "request.auth.basic[&2].value",
            "#string": "request.auth.basic[&2].type"
          }
        }
      },
      "Headers": {
        "#GET": "request.method",
        "*": {
          "*": {
            "$": "request.header[&2].key",
            "@": "request.header[&2].value",
            "#text": "request.header[&2].type"
          }
        }
      },
      "Host": {
        "*": {
          "*": {
            "@": "request.url"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "request": {
        "url": "=join('/', @(2,request.url))"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "request": {
        "url": "=concat(@(1,url), /some-default-value)"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "request": {
        "*": "&1.&",
        "url": "&1.&.raw"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "@": "templateConfig.commonClientConfig.item[]"
    }
  }
]

where

  • join function is used to derive concatenated string from combining each elements of the array without repeatedly mentioning them, while concat is used just to handle the single concatenation of the default string.
  • Notice that the innermost common key is request for the arrays Body, Headers and Host those should be individually treated depending on the need for each, and they are finally accumulated under common nodes(templateConfig.commonClientConfig.item[]) within the last shift spec.

Upvotes: 1

Related Questions