0

I am trying to call an API, and parse the results and then return the parsed results.

Here is my schema in marshmallow:

from marshmallow import Schema, fields, post_load
import json

class MyResponseDataSchema(Schema):
    path = fields.Str(required=False)
    content_match_count = fields.Int(required=False)


class MyResponseSchema(Schema):
    value = fields.List(
        fields.Nested(MyResponseDataSchema),
        required=False,
    )
data = '{value: [{"path" : "libs/shared/components/shortcuts/src/lib/shortcuts.component.ts", "content_match_count" : 3},{"path" : "libs/shared/components/shortcuts/src/lib/shortcuts.module.ts", "content_match_count" : 5}]}'
schema_data = MyResponseSchema(many=True).load(data)

What is wrong with my schema?

  • What have you tried so far? I wonder if [this](https://stackoverflow.com/questions/37237350/is-it-possible-to-validate-list-using-marshmallow) is asking the same question? – larsks May 31 '23 at 18:15
  • @larsks I have updated my question. That does not seem to answer my question as it is assuming I know how to use marshmallow with a list and I do not think I am using it correctly. –  Jun 01 '23 at 17:26
  • First, your schema defines a field named `content_matches`, but your data has a field `content_match_count`. Secondly, `content_match_count` is an integer, but `content_matches` is defined to be a string. Other than that, [this answer](https://stackoverflow.com/a/40900619/147356) directly addresses how to validate a list of items using a schema (`MySchema(many=True).load(data)`). – larsks Jun 01 '23 at 17:31
  • @larsks thank you for the tip. I have updated my question. I am still getting an error. Any other suggestions? –  Jun 01 '23 at 18:19
  • here is the error I am getting now: `marshmallow.exceptions.ValidationError: {'_schema': ['Invalid input type.']}` –  Jun 01 '23 at 18:26
  • Your updated example refers to a variable `data` but does not define any such variable. Please ensure that the example you post is *runnable* so that we can reproduce the error you're asking about. – larsks Jun 01 '23 at 18:33
  • @larsks I updated my question so you should be able to run it now. –  Jun 01 '23 at 18:49
  • here is a codesandbox which can reproduce the error https://codesandbox.io/p/sandbox/keen-waterfall-s80ctu?file=%2Fmain.py%3A29%2C53 –  Jun 01 '23 at 18:53

1 Answers1

0

The problem here isn't with your schema, it's with your data. You're trying to load() a string, but Marshmallow doesn't work that way; you need to pass a dictionary to the .load() method:

from marshmallow import Schema, fields
import json

class MyResponseDataSchema(Schema):
    path = fields.Str(required=False)
    content_match_count = fields.Int(required=False)


class MyResponseSchema(Schema):
    value = fields.List(
        fields.Nested(MyResponseDataSchema),
        required=False,
    )

data = '{"value": [{"path" : "libs/shared/components/shortcuts/src/lib/shortcuts.component.ts", "content_match_count" : 3},{"path" : "libs/shared/components/shortcuts/src/lib/shortcuts.module.ts", "content_match_count" : 5}]}'

obj = json.loads(data)
schema_data = MyResponseSchema().load(obj)

This is shown in the Marshmallow documentation.

Note that in the above code, I've made two changes from your example:

  • I've modified data so that it's valid JSON data.
  • I've removed the many=True from the call to MyResponseSchema() because you've edited your question such that you're no longer trying to parse a list of objects.
larsks
  • 277,717
  • 41
  • 399
  • 399