2

Input JSON that I have to transform is as follows :

{
  "Business": [
    {
      "Label": "Entertainment",
      "category": "Advert",
      "weight": "",
      "types": [
        "T1",
        "T2"
      ]
    },
    {
      "Label": "FMCG",
      "category": "Campaign",
      "weight": "",
      "types": [
        "T9",
        "T10"
      ]
    }
  ]
}

Expected Output :

{
  "Business": [
    {
      "Label": "Entertainment",
      "category": "Advert",
      "weight": "",
      "types": "T1"
    },
    {
      "Label": "Entertainment",
      "category": "Advert",
      "weight": "",
      "types": "T2"
    },
    {
      "Label": "FMCG",
      "category": "Campaign",
      "weight": "",
      "types": "T9"
    },
    {
      "Label": "FMCG",
      "category": "Campaign",
      "weight": "",
      "types": "T10"
    }
  ]
}

I have tried the different JsonSpecs provided at the JOLT github help page. But I am not able to solve this. Any help or pointers will be appreciated.

2
  • 1
    is typess a typo in your input/output samples? Commented Sep 13, 2016 at 4:20
  • 1
    Yes, it was typo. I have corrected it now. Commented Sep 13, 2016 at 4:35

1 Answer 1

8

You have to do two shift operations.

You want to "duplicate" the Label and Category based on how many entries you have in "types" array. So do that first, into a temporary "bizArray".

Also record which "type" goes with that duplicated Label and Category in a temporary "typeArray", that has the same indexes as the bizArray.

In the second shift, "join" the two parallel arrays, "bizArray" and "typesArray" to get your final array.

Spec

[
  {
    "operation": "shift",
    "spec": {
      "Business": {
        "*": { // business array
          "types": {
            "*": { // type array
              "@2": "bizArray[]",  // make a copy of the whole biz object
              "@": "typesArray[]"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "bizArray": {
        "*": { // bizArray index
          "Label": "Business[&1].Label",
          "category": "Business[&1].category",
          "weight": "Business[&1].weight"
        }
      },
      "typesArray": {
        "*": "Business[&].types"
      }
    }
  }
]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Milo, this works !. But I am not able to understand how this works. Documentation on this library is so convoluted that it makes no sense to me. I think I should try for another library.
Nice job! I've never used Jolt, but I took a look at this last night and spent a couple of hours reading the docs and playing around with the demo. I got really close (I knew it needed two shifts) but just couldn't get a correct final product. Jolt is interesting, to say the least!
Thanks. It is a complex thing to doc. A) each operation (on purpose) can have a different DSL, as that is the whole point. Namely that a single DSL to rule them all is basically just Java. B) its recursive, and all operations operate one field at a time. Thus "aggregate" things and dealing with Arrays is "hard" / "obtuse".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.