Search code examples
jsonata

JSONata transformation script for taking source as it is with some override/modifications


I have JSON, for example:

{
 "PDMSReferenceNumber": "11340",
 "OntologyClass": "rdl:P101003917",
 "TopTag": "DEEP1",
 "ServiceDescription2": "Main manual",
 "SystemVoltagePrimaryWinding": "",
 "ClearOpeningHeight": "true"
}

Is it possible to create JSONata script like this:

{
  "*": *,
  "MainTag": TopTag
}

The result should be

{
 "PDMSReferenceNumber": "11340",
 "OntologyClass": "rdl:P101003917",
 "ServiceDescription2": "Main manual",
 "SystemVoltagePrimaryWinding": "",
 "ClearOpeningHeight": "true",
 "MainTag": "DEEP1"
}

So I want to take the source JSON is it is and make some override and modifications.

Thank you!


Solution

  • You could try using the Transform Function - https://docs.jsonata.org/other-operators#-------transform

    So this basic "copys" TopTag to MainTag, and adds to object and then deletes TopTag

    $ ~> |$|{'MainTag': TopTag}, ['TopTag']|
    

    Here you go to show it: https://try.jsonata.org/YqpO6oUk9