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

$var can't be use as accessor of a dict #719

Open
art35rennes opened this issue Nov 25, 2024 · 1 comment
Open

$var can't be use as accessor of a dict #719

art35rennes opened this issue Nov 25, 2024 · 1 comment

Comments

@art35rennes
Copy link

JSONata: 2.0.5

I pass last few day to troubleshouting my expressions and i doesn't find any solution or workaround to this case, i'm starting to think that their is a bug somewhere.

I use try.jsonata.org sandbox to troubleshoot and reproduce this case.

Goal of the code:
I'm looking for replace "natureTravaux" string by human readable string from a dict. After translation, result is encapsulated in a array.

This is my simplified json data:

{
  "listeControle":[
    {
      "conformite": {
        "natureTravaux": "aReparer"
      }
    },
    {
      "conformite": {
        "natureTravaux": "localiserLExutoire"
      }
    }
  ]
}

This is my target result:

{
  "listeControle":[
    {
      "conformite": {
        "natureTravaux": ["A réparer"]
      }
    },
    {
      "conformite": {
        "natureTravaux": ["Localiser l’exutoire"]
      }
    }
  ]
}

This is my code that illustrate the issue:

$map(listeControle, function($controle){
    $merge([
        $controle, {
            "conformite": $merge([
                $controle.conformite,
                {
                    "natureTravaux": [{
                                        "aReparer": "A réparer",
                                        "localiserLExutoire": "Localiser l’exutoire"
                                    }."aReparer",
                                    {
                                        "aReparer": "A réparer",
                                        "localiserLExutoire": "Localiser l’exutoire"
                                    }.$controle.conformite.natureTravaux,
                                    $controle.conformite.natureTravaux,
                                    $controle.conformite.natureTravaux = "aReparer"?1:0
                                    ]
                }
            ])
        }
    ])
})

This is the output of the previous code:

[
  {
    "conformite": {
      "natureTravaux": [
        "A réparer",
        "aReparer",
        "aReparer",
        1
      ]
    }
  },
  {
    "conformite": {
      "natureTravaux": [
        "A réparer",
        "localiserLExutoire",
        "localiserLExutoire",
        0
      ]
    }
  }
]

As you can see, {...}."myString" and {...}.$var doesn't give the same résult even if $var == "myString".

I tried to encapsulated $var with () or [] to forced $var to be computed before accessing, i rewrite complete logic many time but i can't return $var key value on this dict.

Can you help me to find if either JSONata or me is the probleme ?

@markmelville
Copy link
Contributor

markmelville commented Nov 25, 2024

Without any context besides your title, "$var can't be use as accessor of a dict", I would say, yes it can by using the $lookup function:

$lookup($dict,$var)

But when I look at your example, I'm not totally sure about the algorithm you're trying to implement. But based on what I'm seeing I'm going to suggest you use the transform operator. Try this.

(
    $translations:={
        "aReparer": "A réparer",
        "localiserLExutoire": "Localiser l’exutoire"
    };
    $~>|listeControle.conformite|{
        "natureTravaux": [ $lookup($translations,natureTravaux) ]
    }|
)

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

2 participants