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

How to pass string with backslash at end to json_query #323

Open
dhiraam opened this issue Jan 18, 2024 · 1 comment
Open

How to pass string with backslash at end to json_query #323

dhiraam opened this issue Jan 18, 2024 · 1 comment

Comments

@dhiraam
Copy link

dhiraam commented Jan 18, 2024

Below is the JSON output text from a command, trying to fetch FullName and FileVersionRaw where FullName not contains '\msvc\'

{
    "windows_result.output": [
        {
            "FullName": "E:\\\\app\\\\client\\\\product\\\\client\\\\bin\\\\file.txt",
            "Mode": "-a----",
            "Target": [],
            "VersionInfo": {
                "FileVersionRaw": "19.0.0.0",
                "ProductVersionRaw": "0.0.0.0"

            }
        },
        {
            "FullName": "E:\\\\app\\\\client\\\\product\\\\msvc\\\\vc10\\\\file.txt",
            "Mode": "-a----",
            "Target": [],
            "VersionInfo": {
                "FileVersionRaw": "19.0.0.0",
                "ProductVersionRaw": "0.0.0.0"

            }
        }
    ]
}

Below code produces result as we search only for string '\\msvc' which starts with backslash

- name: Print result - Windows
    debug:
      msg: "{{ windows_result.output | to_json | from_json | json_query(client_path) }}"
    vars:
      client_path: "[? !contains(FullName,'\\msvc')].{FullName: FullName, FileVersionRaw: VersionInfo.FileVersionRaw}"

Result output:

    "msg": [
        {
            "FileVersionRaw": "19.0.0.0",
            "FullName": "E:\\\\app\\\\client\\\\product\\\\client\\\\bin\\\\file.txt"
        }
    ]

However if string modified to '\\msvc\\' to search msvc between backslashes, \' at end considered as escape for single quote and throws error. Please help.

@ftnt-dspille
Copy link

The reason it's failing when you add \\ to the end of the msvc is because of how it's getting rendered at the jmespath level. When you have two escapes it gets resolved to a single \, and you see in the error that there is only a single escape right after the \msvc

image

So you need to double the number of backslashes to acount for this: once for the scripting or templating language and once for the JMESPath evaluation. This will work if you change your jmespath query to "[? !contains(FullName,'\\msvc\\\\')].{FullName: FullName, FileVersionRaw: VersionInfo.FileVersionRaw}"

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