How do I deserialize a different structured JSON data in Marshmallow?

Erdin Eray

I have a Schema as below:

from marshmallow import Schema, fields

class ContactSchema(Schema):
    # ... other fields ...
    phone = fields.Str()
    # ... other fields ...

However, JSON data I deal with is different:

{
    // ... other data ...
    "information": {
        "address": "foo",
        "email": "[email protected]",
        "phone": "+101234567890"
    }
    // .. other data ..
}

As you can see, phone key is under information, which is different than how ContactSchema is formed.

Can I, and how can I, map a Field under a Schema to a different path in target JSON data?


Environment

  • Python 3.5 and above
  • Marshmallow 2.16.3
yorodm

You can extend your schema and provide a pre_load method:

class ContactSchema(Schema):

    @preload
    def extract_information(self, data):
       # Please check for None's
       data['phone'] = data['information'].pop('phone')
       return data

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I deserialize a nested JSON array using GSON?

How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)

How do I deserialize a JSON array that contains only values?

How do I deserialize a derived class with json.net

How do I deserialize a JSON array using Newtonsoft.Json

How do I deserialize JSON into a List<SomeType> with Kotlin + Jackson

How do I deserialize a JSON array(flat) and ignore some token

How do I build a class to deserialize following Json into?

How do I deserialize JSON with optional properties?

How do I deserialize a property containing an escaped JSON string?

How do I render different data types from a fetched json?

How do I deserialize a dynamic json property to object?

How do i clear data from a C++ structured array?

How do I deserialize JSON correctly in C#

How do I deserialize the byte stream from a reqwest response into JSON?

How do I add structured data using JSON-LD on dynamic pages that need to wait for content to load?

How to deserialize a JSON array containing different data types to a single object

How do I parse a structured string data(nearing JSON) in textarea to retrieve its required attributes?

How do I deserialize a Json object?

How do I deserialize an array of JSON data into a POJO using an abstract class?

How do I deserialize a JSON array without needing a wrapper type?

How do I deserialize this type of data structure

How do I deserialize to object with json.net (instead of JObject)

How do I deserialize nested JSON data into a flattened object?

How do I call JSON data stored as a variable in a different component?

How to deserialize JSON with different heirs

How do I use Jackson Databind to deserialize the following JSON?

How do I manipulate a messy table into structured data in google sheets?

How to deserialize JSON when the result returns different data type