Skip to content

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

Description

@dhiraam

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions