0

I have the follwoing json structure (see here: https://github.com/sebischair/NLU-Evaluation-Corpora/blob/master/AskUbuntuCorpus.json):

{
        "author": "Kristofer",
        "url": "http://askubuntu.com/questions/226332/how-to-install-a-canon-mf8040cn-printer-on-ubuntu-12-04",
        "text": "How to Install a Canon MF8040Cn Printer on Ubuntu 12.04",
        "entities": [
            {
                "text": "Canon MF8040Cn",
                "entity": "Printer",
                "stop": 5,
                "start": 4
            },
            {
                "text": "12.04",
                "entity": "UbuntuVersion",
                "stop": 11,
                "start": 9
            }
        ],
        "intent": "Setup Printer",
        "answer": {
            "text": "<p>For 14.04 through 16.04 do the following:</p>\n\n<p>Download the drivers from:\n<a href=\"https://www.usa.canon.com/internet/portal/us/home/support/details/printers/black-and-white-laser/mf4770n?tab=drivers#Z7_MQH8HIC0L88RB0AMD0F1Q42K25\" rel=\"nofollow\">https://www.usa.canon.com/internet/portal/us/home/support/details/printers/black-and-white-laser/mf4770n?tab=drivers#Z7_MQH8HIC0L88RB0AMD0F1Q42K25</a></p>\n\n<p>untar it to a directory.\nfrom that directory (assuming 64-bit, adjust as you need to)</p>\n\n<pre><code>cd ~/Downloads\ngunzip -c Linux_UFRII_PrinterDriver_V320_us_EN.tar.gz | tar xvf -\ncd Linux_UFRII_PrinterDriver_V320_us_EN/64-bit_Driver/Debian/\nsudo apt-get -y install libglade2-0 libc6:i386 lib32z1 libxml2:i386 libjpeg62:i386 libstdc++6:i386\ncd 64-bit_Driver/Debian\nsudo dpkg -i *.deb\n</code></pre>\n\n<p>reboot</p>\n\n<p>run add printer .. should just show up automatically when you click the 'Add' button .. give it a few seconds and the printer chirps then just magically shows up.</p>\n\n<p>EDIT 3/2/15 for Vivid (15.04) (I suspect may need for 14.10 as well on brand new installation):</p>\n\n<p>Added extra software dependencies to the above</p>\n\n<p>EDIT 12/18/15 for 15.10 everything worked using above steps. Also, I didn't have to reboot .. it just worked.</p>\n\n<p>EDIT 2/27/16 (16.04beta) Updated the link to the 3.10 driver (they updated their website). Everything just worked using above, didn't need to reboot.</p>\n",
            "author": "JimB"
        },
        "training": false
    }

So you can load this as a dictionary with json.load.

I need to change the order of the key vlaue pairs in the entities-dictionary. Now it is text, entity, stop, start. I want to change the order of every pair. Likewise the "intent" after the entities dict I want to to change the position before this entities dict.

3
  • 3
    Python dicts have no direct interface for changing the order of key-value pairs. Maybe you'd have better luck with collections.OrderedDict? Commented Mar 6, 2018 at 13:37
  • But I have this json file. Then working with json instead with dicts? Commented Mar 6, 2018 at 13:38
  • 1
    If you're saying "but the thing returned by json.load is a json object, not a dictionary", that's a common misconception. There is no independent "json" type; json.load returns a perfectly ordinary dictionary. (or list or string or int, depending on the structure of the data you're loading) Commented Mar 6, 2018 at 13:40

2 Answers 2

2

You can use OrderedDict to control the order of key-value pairs in a dictionary.

from collections import OrderedDict
import json

def ordered(d, desired_key_order):
    return OrderedDict([(key, d[key]) for key in desired_key_order])

toplevel_desired_key_order = ("author", "url", "text", "intent", "entities", "answer", "training")
entity_desired_key_order = ("stop", "start", "entity", "text")

with open("data.json") as file:
    d = json.load(file)

result = ordered(d, toplevel_desired_key_order)
result["entities"] = [ordered(entity, entity_desired_key_order) for entity in result["entities"]]

with open("results.json", "w") as file:
    json.dump(result, file, indent=4)

Result:

{
    "author": "Kristofer",
    "url": "http://askubuntu.com/questions/226332/how-to-install-a-canon-mf8040cn-printer-on-ubuntu-12-04",
    "text": "How to Install a Canon MF8040Cn Printer on Ubuntu 12.04",
    "intent": "Setup Printer",
    "entities": [
        {
            "stop": 5,
            "start": 4,
            "entity": "Printer",
            "text": "Canon MF8040Cn"
        },
        {
            "stop": 11,
            "start": 9,
            "entity": "UbuntuVersion",
            "text": "12.04"
        }
    ],
    "answer": {
        "text": "<p>For 14.04 through 16.04 do the following:</p>\n\n<p>Download the drivers from:\n<a href=\"https://www.usa.canon.com/internet/portal/us/home/support/details/printers/black-and-white-laser/mf4770n?tab=drivers#Z7_MQH8HIC0L88RB0AMD0F1Q42K25\" rel=\"nofollow\">https://www.usa.canon.com/internet/portal/us/home/support/details/printers/black-and-white-laser/mf4770n?tab=drivers#Z7_MQH8HIC0L88RB0AMD0F1Q42K25</a></p>\n\n<p>untar it to a directory.\nfrom that directory (assuming 64-bit, adjust as you need to)</p>\n\n<pre><code>cd ~/Downloads\ngunzip -c Linux_UFRII_PrinterDriver_V320_us_EN.tar.gz | tar xvf -\ncd Linux_UFRII_PrinterDriver_V320_us_EN/64-bit_Driver/Debian/\nsudo apt-get -y install libglade2-0 libc6:i386 lib32z1 libxml2:i386 libjpeg62:i386 libstdc++6:i386\ncd 64-bit_Driver/Debian\nsudo dpkg -i *.deb\n</code></pre>\n\n<p>reboot</p>\n\n<p>run add printer .. should just show up automatically when you click the 'Add' button .. give it a few seconds and the printer chirps then just magically shows up.</p>\n\n<p>EDIT 3/2/15 for Vivid (15.04) (I suspect may need for 14.10 as well on brand new installation):</p>\n\n<p>Added extra software dependencies to the above</p>\n\n<p>EDIT 12/18/15 for 15.10 everything worked using above steps. Also, I didn't have to reboot .. it just worked.</p>\n\n<p>EDIT 2/27/16 (16.04beta) Updated the link to the 3.10 driver (they updated their website). Everything just worked using above, didn't need to reboot.</p>\n",
        "author": "JimB"
    },
    "training": false
}
Sign up to request clarification or add additional context in comments.

Comments

0

@Kevin:

I get for the data example above this error:

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.