skip to Main Content

I have a list of json data called mylist containing a site’s data. I want to append the site’s network logs as a value to the the list in a key value pair. Since the network logs is a large data, I want to save it as a multiline text instead of a single line long string. The output in the stackoverflow container is showing the "networkLogFilePath" key’s value as a multiline text, but it’s not the case with json file. I want to acheive the same with json file.

My code:

  import re
  import json

  with open("dummydict1.json", "w") as 
 f:
  dummyjson = [
            {
                "rank":1,
                
  "bottom_ad_title":
  "Onlinecarparts.co.uk 
  Coupon | New 50% Coupon Code",
                
  "bottom_ad_desc":"Redeem 50% 
 Onlinecarparts.co.uk Coupon Now. 
 Latest Verified Coupons & Deals! 50% 
 Onlinecarparts.co.uk Coupon Limited 
 Time Offer. Onlinecarparts.co.uk 
 Coupons Save Now. Top Discounts. Save 
 Today.", 
                                               
 "bottom_ad_link":
 "https://www.couponscored.com"
            },
            {
                "rank":2,
                "bottom_ad_title":
 "Onlinecarparts.co.uk Coupon | New 
 50% Coupon Code",
                
 "bottom_ad_desc":"Redeem 50% 
 Onlinecarparts.co.uk Coupon Now. 
 Latest Verified Coupons & Deals! 50% 
 Onlinecarparts.co.uk Coupon Limited 
 Time Offer. Onlinecarparts.co.uk 
 Coupons Save Now. Top Discounts. Save 
 Today.",
                
 "bottom_ad_link":
 "https://www.couponscored.com"
            }
        ]

# better_json = re.sub(r'^((s*)".*?":)s*([[{])', r'1n23', json.dumps(dummyjson, indent='t'), flags=re.MULTILINE)
print(better_json)

mylist = [{'result': {"linkText": "https://www.skechers.com/shoe-finder/",
"link": "https://www.skechers.com",
"price": "",
"affiliate": "",
"byAffiliate": "",
"hostName": "www.skechers.com",
"IsCompetitorSite":0}, 
"networkLogFilePath":
json.dumps(dummyjson)}]

# "[" + ",n,".join([json.dumps(d) for d in dummyjson]) + "]"}] # didn't work

# json.dumps(dummyjson, indent=1, separators=(',', ': ')) # didn't work

# pprint.pprint(mylist) # log printed and indented but as of pieces of cropped strings

print(json.dumps(mylist, indent=2))
f.write(json.dumps(mylist, indent=2))

Output:

  [
{
"result": {
  "linkText": 
"https://www.skechers.com/shoe- 
finder/",
  "link": "https://www.skechers.com",
  "price": "",
  "affiliate": "",
  "byAffiliate": "",
  "hostName": "www.skechers.com",
  "IsCompetitorSite": 0
},
"networkLogFilePath": "[{"rank": 1, 
"bottom_ad_title": 
"Onlinecarparts.co.uk Coupon | New 
50% Coupon Code", "bottom_ad_desc": 
"Redeem 50% Onlinecarparts.co.uk 
Coupon Now. Latest Verified coupons & 
Deals! 50% Onlinecarparts.co.uk Coupon 
Limited Time Offer. 
Onlinecarparts.co.uk Coupons Save Now. 
Top Discounts. Save Today.", 
"bottom_ad_link": 
"https://www.couponscored.com"}, 
{"rank": 2, "bottom_ad_title": 
"Onlinecarparts.co.uk Coupon | New 
50% Coupon Code", "bottom_ad_desc": 
"Redeem 50% Onlinecarparts.co.uk 
Coupon Now. Latest Verified Coupons & 
Deals! 50% Onlinecarparts.co.uk Coupon 
Limited Time Offer. 
Onlinecarparts.co.uk Coupons Save Now. 
Top Discounts. Save Today.", 
"bottom_ad_link": 
"https://www.couponscored.com"}]"
 }
]

The output is a well indented json, but the value for key "networkLogFilePath" is a single line of long string in MBs. The container here is showing it as a multiline text but it’s not the same with the .json file. I tried indent = 2 and "t" in json.dumps, re.MULTILINE, pprint.pprint, and joining the values in the networkLogFilePath with "n" but they didn’t work.

Can anyone help me achieve the value for the networkLogFilePath key to printed as string in multiline as shown in the stackoverflow container?

2

Answers


  1. Chosen as BEST ANSWER

    There is a 'word wrap' option in VScode that wraps the large json data to next lines. But the file I have received has that setting pre-enabled exclusively for it. Its long string lines are shown wrapped and its wrap setting can be easily switched on and off with Alt+Z. The newly created json file from the code above don't have the setting and Alt+Z doesn't works.


  2. Okay, there was an option of ‘force enable the features’ like word wrap, etc. which were disabled. Now, the file is too large to view.
    Problem is solved.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search