Python Forum
print python json dump onto multiple lines
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print python json dump onto multiple lines
#1
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\"}"
Reply
#2
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" }
Reply
#3
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"
}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,436 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  JSON Dump and JSON Load foxholenoob 8 979 Oct-12-2023, 07:21 AM
Last Post: foxholenoob
  What are these python lines for? What are tey doing? Led_Zeppelin 7 1,561 Feb-13-2023, 03:08 PM
Last Post: deanhystad
  How to write the condition for deleting multiple lines? Lky 3 1,100 Jul-10-2022, 02:28 PM
Last Post: Lky
  Delete multiple lines from txt file Lky 6 2,207 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Python Split json into separate json based on node value CzarR 1 5,476 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  failing to print not matched lines from second file tester_V 14 5,947 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  geojson to json --missing multiple row output yoshi 9 2,657 Mar-06-2022, 08:34 PM
Last Post: snippsat
  Display table field on multiple lines, 'wordwrap' 3python 0 1,747 Aug-06-2021, 08:17 PM
Last Post: 3python
  python seems to be skipping lines of code alansandbucket 1 4,088 Jun-22-2021, 01:18 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020