Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
jsonstr.format()
#1
the .format() method does not always work with JSON strings. what do you guys use instead?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
More info.

Do you mean pretty printing json, formatting the str into json?
Recommended Tutorials:
Reply
#3
i have code that needs to put a string in where a {} is. it turns out the string where the {} is, is sometimes a string of some JSON. what .format seems to be doing is taking an earlier { and treating everything up a matching } (not sure which it used) as a string index. using .replace('{}',what_to_put_in) works.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
I think you'd be better off parsing the json, and formatting whichever part needs to be formatted, instead of treating json as if it's a string.
Reply
#5
(Apr-02-2018, 07:47 PM)nilamo Wrote: I think you'd be better off parsing the json, and formatting whichever part needs to be formatted, instead of treating json as if it's a string.

that could be a lot of unpredictable parts when it's someone else's json.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
How about the built-in json.tool? the answer to Metuburr's post
cat jsonfile.json | python -m json.tool > formatted_output.txt
Reply
#7
(Apr-03-2018, 02:27 AM)Skaperen Wrote: that could be a lot of unpredictable parts when it's someone else's json.
If it's someone else's json, then string formatting will already be giving you unpredictable results.
Reply
#8
what i need is a .format that can be made to use something else instead of "{}" find where to substitute stuff. but nilamo is right. playing around with strings that could have anything in them can be dangerous.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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