Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Replace transformation not working #23

Closed
cmchaitu opened this issue Dec 5, 2018 · 7 comments
Closed

JSON Replace transformation not working #23

cmchaitu opened this issue Dec 5, 2018 · 7 comments

Comments

@cmchaitu
Copy link

cmchaitu commented Dec 5, 2018

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 cmchaitu changed the title JSON ttransformation not working JSON Replace transformation not working Dec 5, 2018
@pareshjoshi
Copy link

pareshjoshi commented Dec 14, 2018

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

@cmchaitu
Copy link
Author

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

@pareshjoshi
Copy link

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

@cmchaitu
Copy link
Author

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

@pareshjoshi
Copy link

pareshjoshi commented Dec 21, 2018

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.

@PepperDave
Copy link

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

@Metal-Frog
Copy link

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

ttstanley pushed a commit that referenced this issue Feb 6, 2024
Use HTTPS URLs wherever possible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants