1. Matthew Schinckel
  2. django-jsonfield
  3. Issues
Issue #26 new

allow plain string input

created an issue

Under Django 1.5.1 and django-jsonfield 0.9.10, if I enter bare string data into a jsonfield (i.e., a series of alphanumeric characters), I get an error that this isn't valid json. I was under the impression (given input from a former colleague that I inherited a project from) that this is a regression. If that's true, can it be fixed? If not, would it be possible to (optionally) be able to accept something other than strict JSON, if it's parsable directly as a simple string?

Comments (9)

  1. jantman reporter

    Ok, I'm guessing this is probably a miscommunication or misunderstanding then. We're using django-jsonfield for fields that are supposed to be able to store: 1) valid, complete JSON hashes 2) simple strings - i.e. "foo" 3) integers

    When the application was handed over to me, I was given the distinct impression that I should be able to type a bare string (i.e. "foo" without the quotes) into the jsonfield, and have it converted to JSON.

    From your reply, I gather this is incorrect?

  2. Matthew Schinckel repo owner

    Ah, gotcha.

    Eventually, when I get around to using newer postgres versions, I'll be storing jsonfield data in json in-db.

    I'll be interested to see if those can handle any valid json data (that isn't wrapped in an object or array)

  3. Jeremie Pardou


    Accordinly to last RFC, string, boolean and integer are now valid JSON strings. We can have






    as valid values so I guess postgres is going to support this too if it's not already done.

    I can make a PR to fix that if you want.

  4. Jared Morse

    I would also like to see this implemented. I have a situation where I might store an int, str, list or dict.

    Edit: I just realized that it's not that you can't store those things, it's just that if a str is assigned to a JSONField, the field tries to interpret it as JSON. Hmm

  5. Matthew Schinckel repo owner

    Ah, the use of SubFieldBase will break assigning just a plain string to the field though.

    Which means you won't be able to assign the value you get back from the database.

  6. LIU Yu

    Still seeing this issue as of 0.9.15 (Django 1.8.6 / PostgreSQL 9.4). The exception was raised from JSONField.to_python method, looks like it is trying to JSON-deserialize the value as long as it is a string. But it could as well be a native JSON value that happens to be a plain string. If JSONField does not support storing plain strings, could we have this stated in the document?

  7. Log in to comment