Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Recursively transform JSON using jolt

I am trying to transform a JSON into another JSON using JOLT.

My source JSON has the following format (the number of levels of child modules is not known):

{
    "modules": [
        {
            "id": "1",
            "modules": [
                {
                    "id": "1.1",
                    "modules": []
                },
                {
                    "id": "1.2",
                    "modules": [
                        {
                            "id": "1.2.1",
                            "modules": []
                        }
                    ]
                }
            ]
        },
        {
            "id": "2",
            "modules": [
                {
                    "id": "2.1",
                    "modules": []
                }
            ]
        }
    ]
}

My JOLT transformation spec looks like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    {
        "operation": "shift",
        "spec": {
            "modules": {
                "*": {
                    "id": "new_modules[&1].id"
                }
            }
        }
    }
]

The output I get is:

{
    "new_modules": [
      {
        "id": "1"
      },
      {
        "id": "2"
      }
    ]
}

What should I do to get all modules in the target JSON:

{
    "new_modules": [
      {
        "id": "1"
      },
      {
        "id": "1.1"
      },
      {
        "id": "1.2"
      },
      {
        "id": "1.2.1"
      },
      {
        "id": "2"
      },
      {
        "id": "2.1"
      }
    ]
}

I am adding some unrelated text below as stackoverflow complains that my question is mostly code and that I must add more details. Kindly let me know if I am missing details.

>Solution :

You can use this spec:

[
  {
    "operation": "shift",
    "spec": {
      "modules": {
        "*": {
          "id": "new_modules[].&",
          "modules": {
            "*": {
              "id": "new_modules[].&",
              "modules": {
                "*": {
                  "id": "new_modules[].&",
                  "modules": {
                    "id": "new_modules[].&"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

You can’t have infinite levels in the jolt. But you can increase your levels in the above code.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading