Python Forum
print python json dump onto multiple lines - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: print python json dump onto multiple lines (/thread-18092.html)



print python json dump onto multiple lines - lhailey - May-05-2019

I am trying to print the payload variable onto multiple lines. It currently prints on one line.
I am using the indent statement but it still prints on one line.
Can you help?

Step 1:
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:
print '\n' + (json.dumps(payload, indent=4))
Step 3:
Results:
Output:
"{\"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\"}"



RE: print python json dump onto multiple lines - Yoriz - May-05-2019

You have made a json string from a dict and then passing that string to json.dumps.
either just pass the dict directly
import json

print(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))
Output:
{ "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" }
or turn the string back to a dict
import json

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"
                      })


print(json.dumps(json.loads(payload), indent=4))
Output:
{ "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" }



RE: print python json dump onto multiple lines - vishalhule - Mar-02-2020

Note: The json.dumps() method encodes any Python object into JSON formatted String.
Here when you passed dictionary to json.dumps(), it converted it into a JSON formatted string.

When you want to prettyPrint JSON data, we can use indent, sort_keys parameters of dumps() method

Example:
import json
 
print(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=2, sort_keys=True))
Output:
{
  "ip-detunnel": "No",
  "mac-replace-header": "No",
  "mpls-label-stack": "Pass Through",
  "name": "DP800 Slot 1",
  "vlan-tag-insertion": "No",
  "vlan-tag-remove-forward": "Remove"
}