Skip to content

JSON path extraction is handling wildcard tokens incorrectly #14747

Description

@Diluka

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Bug Description

I want to query values in json path

.findAll({where:{'syncData.variants.edges[*].node.sku':{[Op.like]:'test%'}}})

but it generate wrong sql. it quoted the array selectors within fields. \"edges[*]\" should be \"edges\"[*]

Reproducible Example

Here is the link to the SSCCE for this issue:

What do you expect to happen?

WHERE 
  JSON_UNQUOTE(
    JSON_EXTRACT(`syncData`, '$.\"variants\".\"edges\"[*].\"node\".\"sku\"')) LIKE 'test%')
  )

What is actually happening?

WHERE 
  JSON_UNQUOTE(
    JSON_EXTRACT(`syncData`, '$.\"variants\".\"edges[*]\".\"node\".\"sku\"')) LIKE 'test%')
  )

Environment

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

Activity

  1. github-actions commented on Jul 27, 2022

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  2. added
    type: bug
    and removed on Aug 13, 2022
  3. assigned and unassigned on Aug 13, 2022
  4. WikiRik commented on Aug 13, 2022

    @WikiRik
    Member

    I can reproduce this by the following failing test in test/unit/sql/where.test.ts;

            testSql({
              'jsonAttr.edges[*].nested': {
                attribute: 'value',
              },
            }, {
              mysql: `json_unquote(json_extract(\`jsonAttr\`,'$.\\"edges\\"[*].\\"nested\\".\\"attribute\\"')) = 'value'`,
            });

    According to the MySQL docs this is indeed valid JSON path syntax; https://dev.mysql.com/doc/refman/5.7/en/json.html#json-path-syntax

    We should add the wildcard tokens in the jsonPathExtractionQuery function of the MySQL query generator.

    Other dialects might have the same issue but I haven't looked into them.

    If somebody wants to pick this up, feel free!

  5. changed the title [-]json path double quote wrong[/-] [+]JSON path extraction is handling wildcard tokens incorrectly[/+] on Aug 13, 2022
  6. ephys commented on Jan 27, 2023

    @ephys
    Member

    This isn't a bug, the JSON path syntax that sequelize proposes is not the same as the ones the different dialects use, it only supports simple paths (no wildcard) by design to be compatible with all dialects

    In Sequelize 7 (after #15598), you'll be able to write your own json path function to use mysql's syntax:

    import { sql, where, and, Op, Literal } from '@sequelize/core';
    
    function mysqlJsonPath(attribute: string, path: string): Literal {
      return sql`json_unquote(json_extract(${sql.attribute(attribute)}, ${path}))`;
    }
    
    User.findAll({ 
      where: where(
        mysqlJsonPath('syncData', '$.variants.edges[*].node.sku'), 
        Op.like,
        'test%',
      ),
    });
  7. added
    type: feature
    and removed
    type: bug
    on Jan 27, 2023
  8. added theissue type on Mar 9, 2025
  9. removed
    type: feature
    on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions