microsoft / microsoft/json-document-transforms

Replacing specific array item

Open
#64 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PowerShell
Stars
115
Forks
39
PR merge metrics
No merged PRs in 30d

Description

I am having trouble replacing specific array items using the @jdt.path syntax as described on the "Replace" wiki page. The @jdt verbs and attributes don't parse or transform when placed under the array object itself. I was able to get this working by moving the @jdt.replace to the top of the object and then drilling down. Is this a bug?

Sample JSON:

{
  "MyStuff": [
    {
      "Name": "Foo",
      "Value": 123
    },
    {
      "Name": "Bar",
      "Value": 456
    }
  ]
}

The goal is to change the value of the "Foo" item without knowing the ordinal position or how many objects are in the MyStuff array.

Sample transform 1:

{
  "MyStuff": {
    "@jdt.replace": {
      "@jdt.path": "@[?(@.Name == 'Foo')].Value",
      "@jdt.value": 456
    }
  }
}

This should work according to the documentation but all I get is the MyStuff node literally replaced with the transform syntax.

Sample transform 2:

{
  "@jdt.replace": {
    "@jdt.path": "$.MyStuff[?(@.Name == 'Foo')].Value",
    "@jdt.value": 456
  }
}

Alternately, if we place the @jdt.replace at the root and change @jdt.path to start from the root and navigate into MyStuff, it works as expected.

I'm using Visual Studio 2022 + SlowCheetah 4.0.50 in a .NET Framework 4.7.2 project.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Replace wiki page and reproduce Sample transform 1 against the provided JSON, then compare it with the working root-level transform. Trace how an array-relative @jdt.path is parsed and applied; done when the Foo item's Value changes without relying on its ordinal position.

Written by the indexing model from the issue text.

Assessment

Tech stack
json, powershell
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.