1

Re-asking clearly my question : I want to produce this json output using flask.jsonify, how can I build the corresponding dictionary to do so ?

{
  "cluster": {
    "members": [
      {
        "name": "host1",
        "disks": [
          {
            "fstype": "btrfs",
            "size": "62G",
            "status": "up"
          },
          {
            "fstype": "btrfs",
            "size": "260G",
            "status": "up"
          },
          {
            "fstype": "btrfs",
            "size": "263G",
            "status": "up"
          },
          {
            "fstype": "btrfs",
            "size": "257G",
            "status": "up"
          }
        ]
      },
      {
        "name": "host2",
        "disks": [
          {
            "fstype": "btrfs",
            "size": "66G",
            "status": "up"
          },
          {
            "fstype": "btrfs",
            "size": "259G",
            "status": "up"
          }
        ]
      },
      {
        "name": "host3",
        "disks": [
          {
            "fstype": "btrfs",
            "size": "62G",
            "status": "up"
          },
          {
            "fstype": "btrfs",
            "size": "259G",
            "status": "up"
          },
          {
            "fstype": "btrfs",
            "size": "257G",
            "status": "up"
          },
          {
            "fstype": "btrfs",
            "size": "263G",
            "status": "up"
          }
        ]
      }
    ],
    "name": "MyCluster1",
    "status": "HEALTH_OK"
  }
}

Re-asking clearly my question : I want to produce this json output using flask.jsonify, how can I build the corresponding dictionary to do so ?

r3dlight
  • 101
  • 8
  • 3
    Did you read through the [Python library manual for the json module](http://docs.python.org/3.3/library/json.html)? –  Feb 19 '14 at 10:28
  • 1
    Where are you getting the data from to go into the dictionary? Do you know how to use lists and dictionaries? Is the problem building the structure, or processing the data? – jonrsharpe Feb 19 '14 at 10:29
  • 1
    If you want to know how to build the above structure in Python: that essentially is already Python. Assign it to a variable and you're done. –  Feb 19 '14 at 10:30
  • maybe duplicate from http://stackoverflow.com/questions/7907596/json-dumps-vs-flask-jsonify ? – xhallix Feb 19 '14 at 10:34
  • Sorry, I guess I wasn't clear, actually my problem is just to build the above structure using lists and dictionary. – r3dlight Feb 19 '14 at 11:20
  • Could you edit your question to make that clear (for future readers), please? You are actually saying you want to use `jsonify()` so the comment and the question seem to be contradictory to each other. – kkuilla Feb 19 '14 at 11:58

2 Answers2

2

If your long text field is a string called 'text', then you can load it as a json object like this:

import json
new = json.loads(text)
philshem
  • 24,761
  • 8
  • 61
  • 127
  • The OP is asking about the other direction (i.e. turning a Python `dict` into JSON). So he might use `json.dumps()`, but he’s already indicated he’s using `flask.jsonify()` — at least, I assume it’s Flask’s `jsonify()` function. – al45tair Feb 19 '14 at 10:31
  • yes I'm using flask.jsonify to do so... but my question was how to build my dictionary (with the above exemple) ? – r3dlight Feb 19 '14 at 11:27
1

If you’re using flask.jsonify() (I assume you are, from the question) you need to do

flask.jsonify(**my_dict)

rather than flask.jsonify(my_dict).

al45tair
  • 4,405
  • 23
  • 30