简体   繁体   中英

JSON Transformation using JOLT: Nested Array and Concatenation

I have the following input JSON

[
  {
    "name": "Project1",
    "Addresses": [
      [
        "B24",
        "Niyam Street",
        "67897",
        "New York"
      ],
      [
        "A14",
        "Prinston Str",
        "London"
      ]
    ]
  },
  {
    "name": "Project2",
    "Addresses": [
      [
        "123",
        "Portland Street",
        "234"
      ],
      [
        "Lalbag",
        "Kolaba"
      ],
      [
        "8th Avenue",
        "3rd Signal"
      ]
    ]
  }
]

I would like to transform it into like the below:

[
  {
    "name": "Project1",
    "Addresses": [
      "B24 Niyam Street 67897 New York",
      "A14 Prinston Str London"
    ]
  },
  {
    "name": "Project2",
    "Addresses": [
      "123 Portland Street 234",
      "Lalbag Kolaba",
      "8th Avenue 3rd Signal"
    ]
  }
] 

The Addresses attribute value is a two-dimensional array of dynamic size.

Could you please help me with a valid jolt spec or some hints to achieve it? I am lost. Thank you so much.

You can use combination of shift and modify transformations such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&", // the attributes other than "Addresses"
        "Addresses": {
          "*": "&1.&2.&" // determine the objects with indexed key names(0,1,2...) where &2 represents going two level up the tree to get the outermost indices of the objects of the main array, & for indices of the "Addresses" array, and &1 to keep the key name "Addresses" to transfer to the upcoming specs 
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "*": "=join(' ',@(1,&))" // concatenate all components of each array respectively at once 
        }
      }
    }
  },
  {
  // combine the attributes back again
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[&].&1"
      },
      "A*": {
        "*": {
          "*": "[&1].&2"
        }
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM