Skip to content
This repository was archived by the owner on Feb 23, 2026. It is now read-only.
This repository was archived by the owner on Feb 23, 2026. It is now read-only.

Provide a from_dict method #152

Closed
Closed

Description

@tswast

Similar to #151, it would be quite useful to be able to parse a message from a JSON-representation dictionary.

Currently, in https://github.com/googleapis/python-bigquery/pull/332/files we need to deserialize the response to a dictionary because of how the handwritten clients are structured, but then immediately re-serialize the response to a JSON string so that we can call from_json.

Activity

  1. software-dov commented on Oct 19, 2020

    @software-dov
    Contributor

    Effective duplicate of #151 lumping in under that.

  2. software-dov commented on Oct 19, 2020

    @software-dov
    Contributor

    The message constructor allows just passing in a dict; it already does all the heavy lifting.
    E.g.

    class Squid(proto.message):
        mass_kg = proto.Field(proto.INT32, number=1)
    
    s = Squid({"mass_kg": 20})
  3. tswast commented on Oct 19, 2020

    @tswast
    Author

    Does this work with nested message? I recall in the monolith it didn't work the same as the parse from dict method.

  4. busunkim96 commented on Oct 19, 2020

    @busunkim96
    Contributor

    @tswast It should work. This is what allows request to be passed as a dict to the service methods. This KMS sample has some nesting: https://github.com/googleapis/python-kms/blob/c349e3efa0b6cf5dbb9043043310ef5fac0fccc5/samples/snippets/update_key_add_rotation.py#L43-L57

  5. Ark-kun commented on Dec 5, 2022

    @Ark-kun

    The message constructor allows just passing in a dict; it already does all the heavy lifting.

    That constructor is incompatible with message.to_dict and the actual REST API payloads. Meanwhile, .from_json is compatible with .to_json

    This works:

    batch_v1.Job.from_json('{"taskGroups": [{}]}')
    

    This does not work:

    batch_v1.Job({"taskGroups": [{}]})
    
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-145-58ddc394e1ed> in <module>
    ----> 1 batch_v1.Job({"taskGroups": [{}]})
    
    /opt/conda/lib/python3.7/site-packages/proto/message.py in __init__(self, mapping, ignore_unknown_fields, **kwargs)
        564 
        565                 raise ValueError(
    --> 566                     "Unknown field for {}: {}".format(self.__class__.__name__, key)
        567                 )
        568 
    
    ValueError: Unknown field for Job: taskGroups
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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