Jolt specification to transform json into name value pair

Viewed 124

I am trying to convert below JSON into name value pair :

{
  "Size": "2",
  "done": "true",
  "records": [
    {
      "Id": "a7g6s0000004GZuAAM",
      "NN": "00096411.0",
      "Name": "ISOLIN TRADE & INVEST",
      "RecordType": {
        "attributes": {
          "type": "TestType"
        },
        "Name": "Term"
      }
    },
    {
      "Id": "a7g6s0000004GZzAAM",
      "Number": "00096412.0",
      "Name": "ISOLIN"
    }
  ]
}

Expecting output JSON :

{
  "Size" : "2",
  "done" : "true",
  "Items" : [ {
    "Fields" : [ {
      "Name" : "Id",
      "Value" : "a7g6s0000004GZuAAM"
    }, {
      "Name" : "NN",
      "Value" : "00096411.0"
    }, {
      "Name" : "Name",
      "Value" : "ISOLIN TRADE & INVEST"
    }, {
      "Name" : "RecordType_Name",
      "Value" : "Term"
    } ]
  }, {
    "Fields" : [ {
      "Name" : "Id",
      "Value" : "a7g6s0000004GZzAAM"
    }, {
      "Name" : "Number",
      "Value" : "00096412.0"
    }, {
      "Name" : "Name",
      "Value" : "ISOLIN"
    } ]
  } ]
}

I am using below jolt spec but transformation of RecordType element is not as expected :

Jolt Spec :

[
  {
    "operation": "remove",
    "spec": {
      "records": {
        "*": {
          "attributes": " "
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "records": {
        "*": {
          "*": {
            "$": "Items.&2.[#2].Name",
            "@": "Items.&2.[#2].Value"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "Items": {
        "*": {
          "*": "Items.[#2].Fields[]"
        }
      }
    }
  }
]

How can I transform this into required format ?

1 Answers

I think remove transformation is not needed, and better to index each Field.

The trick for the solution would be distinguish RecordType attribute from the others by use of

"RecordType": {"Name": "Field&2.&1\\_&"}

such as

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "records": {
        "*": {
          "*": "Field&1.&", 
          "RecordType": {
            "Name": "Field&2.&1\\_&" 
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "Field*": {
        "*": {
          "$": "Items[0].&2.[#2].Name",
          "@": "Items[0].&2.[#2].Value"
        }
      }
    }
  }
]

enter image description here

Related