Skip to content

add platform agnostic astext (and maybe asint) operators to JSON #4276

Closed

Description

@sqlalchemy-bot

Migrated issue, originally created by Michael Bayer (@zzzeek)

see zzzeek/sqlalchemy#443, #3859, and https://groups.google.com/forum/#!topic/sqlalchemy/SohtCZ6zmDs which all refer to the need for a high quality means of pulling out the data of a JSON element as a plain textual value.

Activity

  1. sqlalchemy-bot commented on Aug 1, 2018

    @sqlalchemy-bot
    CollaboratorAuthor

    Michael Bayer (@zzzeek) wrote:

    can be pushed up if I can get development help

  2. sqlalchemy-bot commented on Aug 1, 2018

    @sqlalchemy-bot
    CollaboratorAuthor

    Changes by Michael Bayer (@zzzeek):

    • removed labels: high priority
  3. sqlalchemy-bot commented on Aug 1, 2018

    @sqlalchemy-bot
    CollaboratorAuthor

    Changes by Michael Bayer (@zzzeek):

    • changed milestone from "1.3" to "1.4"
  4. added this to the 1.4 milestone on Nov 27, 2018
  5. added
    datatypes
    on Dec 1, 2018
  6. Nutigoodpipes commented on May 13, 2019

    @Nutigoodpipes

    Hello, I'm working back on this issue.
    There is something I have trouble to figure out regarding the Comparator class combined with TypeDecorator that looks similar to #3859.

    Concretely, when I define :

    SQLJSON = sqlalchemy.types.JSON()\
        .with_variant(sqlalchemy.dialects.postgresql.JSONB(), "postgresql")
    

    Even with a postgresql engine, the Comparator class in use seems to be the one from the base JSON. Calling the astext property raise an AttritubeError.

    Do you have pointers of where I should look at ?

  7. zzzeek commented on May 13, 2019

    @zzzeek
    Member

    with_variant refers to the type object that takes place when the statement is passed to the compiler for conversion to SQL as well as when the compiled object is executed, where it is used to handle bound parameters and result values. However it cannot be used to impact the in-python Comparator behavior because the statement object is agnostic of any dialect at that point. that is, with_variant has no way to know that when you call column.astext it should be reaching into the "postgresql" variant. so with_variant cant be used in this way.

  8. zzzeek commented on Nov 7, 2019

    @zzzeek
    Member

    this should not be "astext" - the issue as shown in #4969 is that our use of JSON_QUOTE for sqlite is broken, as we don't correctly compare json to json in any case with that (note JSON_QUOTE uses no whitespace), we don't correclty compare booleans, so string/int/numeric just happen to work with that awkward "CAST" recipe but this is foolish. there shouldn't need to be a cast. the left side of a comparison should have dialect-specific ways of extracting the known JSON types out, and then there should be directives so that this can be supplied, like table.c.col['somekey'].to_boolean, to_string, to_int, to_numeric, to_json. then perhaps we can automatically derive these when used in a comparison, but that's optional.

  9. sqla-tester commented on Nov 7, 2019

    @sqla-tester
    Collaborator

    Mike Bayer has proposed a fix for this issue in the master branch:

    Add type accessors for JSON indexed/pathed element access https://gerrit.sqlalchemy.org/1560

  10. sqla-tester commented on Nov 10, 2019

    @sqla-tester
    Collaborator

    Mike Bayer has proposed a fix for this issue in the rel_1_3 branch:

    Add type accessors for JSON indexed/pathed element access https://gerrit.sqlalchemy.org/1572

  11. added a commit that references this issue on Nov 11, 2019
    f26af5d
  12. added a commit that references this issue on Dec 29, 2019
    cfa822f
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

    datatypesfeaturesql

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions