Sven Rojek
Sven Rojek

Reputation: 5858

Django import/reformat JSON into Model

I like to periodically import json data into a Django. What's the best way to import the data below to a Django model.

As far as I've seen is the best approach to use Django fixtures. Since my Model looks like this and I can't change the given JSON, what's the best way to import the data or restructure the JSON to use it as a fixture.


Django Model

class Item(models.Model):
    item_name = models.CharField(max_length=250)
    timestamp = models.DateTimeField()
    value01 = models.DecimalField(max_digits=7, decimal_places=2)
    value02 = models.DecimalField(max_digits=7, decimal_places=2)

Data I like to store:

Item1  |  2017-04-18 09:24:46  |  15.00 | 12.68
Item1  |  2017-04-18 09:25:44  |  14.92 | 12.42

Given JSON:

[
  {
    "id": 1,
    "timestamp": "2017-04-18 09:24:46",
    "details": {
      "id": 843,
      "color": "white"
    },
    "item": {
      "id": 1691,
      "category": "1",
      "item_type": {
        "id": 14,
        "name": "Item1"
      }
    },
    "itemdatavalues": [
      {
        "id": 227,
        "value": "15.00"
      },
      {
        "id": 228,
        "value": "12.68"
      }
    ]
  },
  {
    "id": 2,
    "timestamp": "2017-04-18 09:25:44",
    "details": {
      "id": 843,
      "color": "white"
    },
    "item": {
      "id": 161,
      "category": "1",
      "item_type": {
        "id": 14,
        "name": "Item1"
      }
    },
    "itemdatavalues": [
      {
        "id": 283,
        "value": "14.92"
      },
      {
        "id": 284,
        "value": "12.42"
      }
    ]
  }
]

Upvotes: 1

Views: 2025

Answers (2)

JayHelton
JayHelton

Reputation: 91

This might help: https://docs.djangoproject.com/en/1.11/ref/models/instances/. You should be able to create a new model object in whatever methods you are receiving that json in, then parse whatever json values you want into the object. There are some pretty easy json libraries for python. That is, unless you are wanting to handle something differently, or I am reading the documentation wrong. Have a good day!

Upvotes: 1

FamousJameous
FamousJameous

Reputation: 1583

I wrote a simple script to parse the input file and generate a fixture:

import json
import copy

with open('/tmp/data.json') as dataf, open('/tmp/output.json', 'w') as out:
    data = json.load(dataf)
    newdata = []
    for i, block in enumerate(data):
        new = dict(model="appname.Item", pk=block['id'])
        new['fields'] = dict(item_name=block['item']['item_type']['name'],
                             timestamp=block['timestamp'],
                             value01=block['itemdatavalues'][0]['value'],
                             value02=block['itemdatavalues'][1]['value'])
        newdata.append(copy.deepcopy(new))
    json.dump(newdata, out, indent=2)

Using your input, this results in:

[
  {
    "pk": 1,
    "model": "appname.Item",
    "fields": {
      "item_name": "Item1",
      "timestamp": "2017-04-18 09:24:46",
      "value01": "15.00",
      "value02": "12.68"
    }
  },
  {
    "pk": 2,
    "model": "appname.Item",
    "fields": {
      "item_name": "Item1",
      "timestamp": "2017-04-18 09:25:44",
      "value01": "14.92",
      "value02": "12.42"
    }
  }
]

Which is suitable for use as a fixture. Obviously, you will have to fix the appname.

Upvotes: 2

Related Questions