0

I have the following model:

class BaseTransaction(models.Model):
    """Model to store JSON data"""
    name = models.CharField(max_length=255)
    json_data = JSONField(null=True)

If I create an instance with the following data:

 base_transaction = models.BaseTransaction.objects.create(
            name="Test Transaction",
            json_data={{"sales": 3.24, "date": "2020-06-05"},
                       {"sales": 5.50, "date": "2020-06-04"},
                       {"sales": 256.53, "date": "2020-06-02"}}
        )

How would I access the second row of data without a key? Or is this the wrong format for JSON? I am using this format because the original data is from a CSV and this is how it converts to JSON.

0

1 Answer 1

1

No, the above structure is not inJSON format. You can always validate if it's JSON or not using JSON Formatter & Validator

You would want to restructure is according to the rules of JSON, and manually if that can be done so. Once it's in JSON format, you can access the second row without keys using a for loop and a counter, e.g.

counter = 0    
for (key in obj) {
    counter+=1
    if (counter == 2): 
        # Do anything 
    else:
       print("Key: " + key)
       print("Value: " + obj[key])
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.