1

I got two json objects that I need to do sorts of ORM operations, such as count, filter, all

Here is the first object comments:

in views.py

 comments_response = requests.get('https://jsonplaceholder.typicode.com/comments')
 comments_data = json.loads(comments_response.text)

so below is what print(comments_data) looks like:

 [
      {
        "userId": 1,
        "id": 1,
        "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
        "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
      },
      {
        "userId": 1,
        "id": 2,
        "title": "qui est esse",
        "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
      },
      
    ]

This is second json object: in views.py

posts_response = requests.get(
    'https://jsonplaceholder.typicode.com/posts')
posts_data = json.loads(posts_response.text) 

so below is what print(posts_data) looks like:

[
  {
    "postId": 1,
    "id": 1,
    "name": "id labore ex et quam laborum",
    "email": "Eliseo@gardner.biz",
    "body": "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium"
  },
  {
    "postId": 1,
    "id": 2,
    "name": "quo vero reiciendis velit similique earum",
    "email": "Jayne_Kuhic@sydney.com",
    "body": "est natus enim nihil est dolore omnis voluptatem numquam\net omnis occaecati quod ullam at\nvoluptatem error expedita pariatur\nnihil sint nostrum voluptatem reiciendis et"
  },
]

Is it possible to use django ORM to the json object? such as comments_data.objects.count('title') or comments_posts.objects.all(). Does serializer from DRF can assist in this kind of operations? Do I need to use any other frameworks just to convert the json into ORM-able objects.?

Note: I did search related topics/questions, but most of them have the JSON data from their own internal database of which they have their total access (meaning they can already do ORM from start). In my case, I just received json objects from the external API and it is not from my internal database or any database that I have access from.

Some questions Ive seen but not answering my question:

1.https://stackoverflow.com/questions/66223066/django-getting-values-from-postgres-json-field

2.https://stackoverflow.com/questions/10445176/how-to-write-a-query-to-get-find-value-in-a-json-field-in-django

3.https://stackoverflow.com/questions/36389871/django-jsonfield-filtering

shanksfk
  • 98
  • 7

1 Answers1

2

You have a few options.

  1. Load the data into a model that matches the json, if it's consistent for each record.
  2. Load the data into a JSON field on a django model.
  3. Just use a comprehension to filter your dicts.

For (3), once you've loaded the JSON into a list of python dicts you can manipulate it.

So:

  • Count the records: len(comments_data)
  • All the records: comments_data
  • Filter the records: [item for item in comments_data if item["email"] == "Eliseo@gardner.biz"]

etc.

Basically, there's no need to try and replicate ORM stuff here.

There are a couple of options to load data into Django models:

At the very basic level, if you have a model that matches a json record, you can create an instance and then save it. eg.

# You have the model

class TestModel(models.Model): 
    field_one = CharField(...)
    field_two = IntegerField(...)

# And the JSON: 

json_data = {
    "field_one": "This is the first rec",
    "field_two": 1,
}

# You can create an instance of the model class and save it

instance = TestModel(**json_data)
instance.save()

For more information on translating json into django models and vice-versa, I suggest you read up on serializers.

michjnich
  • 2,796
  • 3
  • 15
  • 31
  • There certainly are many use cases, though I just mentioned are the simplest one. For example, its very hard to do comprehension to do inner join and count together if the two json object has relationships. – shanksfk Dec 06 '22 at 19:26
  • I answered the case you gave, not a different one. If you want to start joining JSON, load it into proper models. – michjnich Dec 07 '22 at 08:41
  • And would you suggest how to that? I'm dealing with lots of JSON and easiest way to process I know is to do ORM on it. – shanksfk Dec 07 '22 at 11:00
  • If you want to use django orm on data, you need to load it into models. – michjnich Dec 07 '22 at 11:01
  • Do you mind sharing the details of how to "load" it into models? using comprehension? using libraries? maybe you can add it into your answer. – shanksfk Dec 07 '22 at 13:59
  • I put some suggestions in, but I can't decide exactly how you solve this issue - it depends on your use case. I strongly suggest you read up on how to use serializers - that's at the core of your solution. – michjnich Dec 07 '22 at 14:49
  • Thanks for your answer, however I just found out that this specific problem can actually be solved by using [FastApi](https://fastapi.tiangolo.com/) or [Django Ninja](https://django-ninja.rest-framework.com/). They both able to ORM the Json data without actually need to store any data in database – shanksfk Dec 08 '22 at 03:59