json-document-transforms icon indicating copy to clipboard operation
json-document-transforms copied to clipboard

JSON Replace transformation not working

Open cmchaitu opened this issue 5 years ago • 7 comments

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 avatar Dec 05 '18 17:12 cmchaitu

@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.

pareshjoshi avatar Dec 14 '18 20:12 pareshjoshi

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" } } }

cmchaitu avatar Dec 14 '18 21:12 cmchaitu

You may want to close the issue if it worked for you.

pareshjoshi avatar Dec 16 '18 07:12 pareshjoshi

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" } ] } }

cmchaitu avatar Dec 19 '18 05:12 cmchaitu

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.

pareshjoshi avatar Dec 21 '18 07:12 pareshjoshi

Does this work? I am trying to replace array elements in appsettings.json and it is failing using the example syntax referred to above

PepperDave avatar Jul 20 '22 09:07 PepperDave

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" } ] }

Metal-Frog avatar Oct 19 '22 13:10 Metal-Frog