question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON field do not survive import & export.

See original GitHub issue

Describe the bug JSON fields do not survive import/export.

To Reproduce Steps to reproduce the behavior:

  1. Have a model with a JSON field containing some JSON e.g. {"a":"b"}
  2. Export using format CSV, JSON or TSV.
  3. Open up new environment.
  4. Import previous file
  5. the json field contains the JSON in quotations and single quotes "{'a':'b'}"

Versions (please complete the following information):

  • Django Import Export: 2.4.0
  • Python 3.9
  • Django 3.1.2

Expected behavior The field should have the same content as before.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
BSVoglercommented, Feb 15, 2021

@aureliendebord

I call this method when I run into a type error when using this field post-import.

def repair_json(djangoobject, field: str):
    """
    Repairs broken import/export strings.
    @param djangoobject: the broken django object, has side effect, as it might be repaired
    @param field: name of the field to repair
    @return:
    """
    # check if there is a chance that it is broken
    if hasattr(djangoobject, field) and (
            type(getattr(djangoobject, field)) == JSONString or type(getattr(djangoobject, field)) == str):
        jsonstring = getattr(djangoobject, field)
        jsonstring = jsonstring.removeprefix("\"")
        jsonstring = jsonstring.removesuffix("\"")
        jsonstring = jsonstring.replace("\'", "\"")
        jsonstring = jsonstring.replace("\\\"", "\"")
        jsonstring = jsonstring.replace("False", "false")
        jsonstring = jsonstring.replace("True", "true")
        setattr(djangoobject, field, json.loads(jsonstring))
        # even if validation will fail, this was improved
        djangoobject.save()
0reactions
matthewhegartycommented, Nov 2, 2021

Hi @takkaria

Many thanks for the update on this issue. Based on the fact that neither of us can reproduce this in 2.6.1 I will close the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DJango - Export Json Field in Model to CSV along with other ...
I solved the issue, and here is how I did it for anyone who might need it. First import the following, from django.db...
Read more >
Advanced field editing using JSON | Cloud automation Cloud
The More options field on automation actions in Jira Cloud allow you to specify a valid JSON object reference and edit issue fields....
Read more >
How to import/export JSON data using SQL Server 2016
This article will explain how to import JSON data into SQL Server 2016 table and how to export data from SQL Server 2016...
Read more >
RSS setting not exported and imported on new sites
When I have done my settings and export the settings.ini file and build a new site and import that file again, it does...
Read more >
Velo Tutorial: Importing and Exporting Collection Data with Code
The API will not check the data you are importing to ensure it matches the Field Type of the field you are importing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found