1

I am trying to print a json dump onto multiple lines. Currently it is printing on one line.

Here are the steps I am attempting;

Step 1: This is the code that I want to print

payload = json.dumps({"ip-detunnel": "No",
                      "name": "DP800 Slot 1",
                      "vlan-tag-insertion": "No",
                      "vlan-tag-remove-forward": "Remove",
                      "mac-replace-header": "No",
                      "mpls-label-stack": "Pass Through"
                      })

Step 2: This is how I am trying to print

print '\n' + payload

Step 3: Here is the Result:

{"vlan-tag-insertion": "No", "name": "DP800 Slot 1", "mpls-label-stack": "Pass Through", "vlan-tag-remove-forward": "Remove", "ip-detunnel": "No", "mac-replace-header": "No"}

My goal is to print the results on multiple lines so that it looks like the json.dumps in step 1

joe john
  • 9
  • 3

1 Answers1

3

Pass indent=4 to dumps

payload = json.dumps({"ip-detunnel": "No", "name": "DP800 Slot 1", "vlan-tag-insertion": "No", "vlan-tag-remove-forward": "Remove", "mac-replace-header": "No", "mpls-label-stack": "Pass Through" }, indent=4)

Or any other number other than 0. Each nesting of the json object will be indented by that many spaces before it. Output:

>>>print(payload)
{
    "ip-detunnel": "No",
    "name": "DP800 Slot 1",
    "vlan-tag-insertion": "No",
    "vlan-tag-remove-forward": "Remove",
    "mac-replace-header": "No",
    "mpls-label-stack": "Pass Through"
}

rdas
  • 20,604
  • 6
  • 33
  • 46
  • I am still having trouble and I get one line return >payload = json.dumps({"ip-detunnel": "No",\"name": "DP800 Slot 1", "vlan-tag-insertion": "No", "vlan-tag-remove-forward": "Remove", "mac-replace-header": "No", "mpls-label-stack": "Pass Through" }) – joe john May 05 '19 at 14:38