json-document-transforms
json-document-transforms copied to clipboard
JSON Replace transformation not working
I'm trying to transform
{ "outputs": [
{ "type": "Sample" },
{
"type": "Sample1",
"indexNamePrefix": "Sample2",
"serviceUri": "Sample3",
"eventDocumentTypeName": "Sample4"
}
]
}
into something like this
{ "outputs": [
{ "type": "Sample" },
{
"type": "Sample1",
"indexNamePrefix": "Sample2",
"serviceUri": "NewValue",
"eventDocumentTypeName": "Sample4"
}
]
}
I have tried the couple of transforms based on your examples Try 1: I just tried to replace the serviceUri which was the only change, following the transform
{
"outputs": {
"@jdt.replace": {
"@jdt.path": "@[?(@.serviceUri == Sample3)]",
"@jdt.value": {
"serviceUri": "NewValue"
}
}
}
}
Result was something like below , which is similar to the transform and doesn't seem like transformation happened
{
"outputs": {
"@jdt.replace": {
"@jdt.path": "@[?(@.serviceUri == Sample3)]",
"@jdt.value": {
"serviceUri": "NewValue"
}
}
}
Try 2: I tried to replace the entire outputs node
{
"outputs": {
// Double brackets are needed to specify
// the array as the transformation value
"@jdt.replace": [
[
{
"type": "Sample"
},
{
"type": "Sample1",
"indexNamePrefix": "Sample2",
"serviceUri": "NewValue",
"eventDocumentTypeName": "Sample4"
}
]
]
}
}
Result was the same as the transform here too , am I missing something ? because the basic remove and rename transforms are working fine.
Thanks in Advance
@cmchaitu It looks like jpath for the "current element" does not work. The example given in the document does not work. For your specific case, change your transform rule to:
{
"@jdt.replace": {
"@jdt.path": "$.outputs[?(@.serviceUri == 'Sample3')]",
"@jdt.value": {
"serviceUri": "New Value"
}
}
}
Note: this overrides the entire object where serviceUri
condition is met.
Thanks , it works . Yes it overrides the entire object but I can do something like below : { "@jdt.replace": { "@jdt.path": "$.outputs[?(@.serviceUri == 'Sample3')]", "@jdt.value": { "type": "Sample1", "indexNamePrefix": "Sample2", "serviceUri": "New Value" "eventDocumentTypeName": "Sample4" } } }
You may want to close the issue if it worked for you.
Hi , I I'm unable to use jdt.replace twice , it says Warning - A member with the name "@jdt.replace" already exists. I'm trying to change value of serviceuri of outputs and inputs
{ "outputs": [ { "type": "Sample" }, { "type": "Sample1", "indexNamePrefix": "Sample2", "serviceUri": "NewValue", "eventDocumentTypeName": "Sample4" } ], { "inputs": [ { "type": "Sample" }, { "type": "Sample1", "indexNamePrefix": "Sample2", "serviceUri": "NewValue", "eventDocumentTypeName": "Sample4" } ] } }
Yes, it wont allow, because you have two @jdt.replace
at same level. The best solution is to understand why "@jdt.path": "@[?(@.serviceUri == Sample3)]",
does not work and fix that.
I may try debug this in my free time (don't know when that would be). Can clone the repo and try and debug? Take example from the documentation.
Does this work? I am trying to replace array elements in appsettings.json and it is failing using the example syntax referred to above
You can use an array: { "@jdt.replace": [ { "@jdt.path": "$.att1", "@jdt.value": "some value" }, { "@jdt.path": "$.att2", "@jdt.value": "also a value" }, { "@jdt.path": "$.att3", "@jdt.value": "third value" } ] }