LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2018, 06:51 AM   #1
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Rep: Reputation: 30
Parsing JSON like string in Python


I'm trying to sanitise JSON like output before inserting into a list. For some reason the white space isn't even stripped out and commas and other artifacts are also inserted. Can anyone see why?

Code:
from subprocess import run, PIPE

output="""
[
    "Str1",
    "Str2"
]"""

output_string = output.strip()

bad_chars = '(){}<>[],\"'
for c in bad_chars: s = output_string.replace(c, "")
#https://stackoverflow.com/questions/3900054/python-strip-multiple-characters

results = list()
results.extend(output_string.split('\n'))

print(results)
 
Old 11-09-2018, 09:02 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,263

Rep: Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339Reputation: 5339
This works:

Code:
from subprocess import run, PIPE

output="""
[
    "Str1",
    "Str2"
]"""

output_string = output.strip()

bad_chars = '(){}<>[]," '
for c in bad_chars:
    output_string = output_string.replace(c, "")
#https://stackoverflow.com/questions/3900054/python-strip-multiple-characters

results = list()
results.extend(s for s in output_string.split('\n') if s.strip())

print(results)
The first mistake was assigning the results of the replace method (which returns a new string) to "s".

That said, you said the string was only "JSON-like", but in you're code sample it's actually valid JSON. So the Pythonic approach would have been this:

Code:
import json

output="""
[
    "Str1",
    "Str2"
]"""

print(json.loads(output))

Last edited by dugan; 11-09-2018 at 09:11 AM.
 
2 members found this post helpful.
Old 11-09-2018, 10:40 AM   #3
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Original Poster
Rep: Reputation: 30
Thanks, I didn't know you could just do that, looks like I'm just too old school!
 
  


Reply

Tags
python3



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] jq, parsing jolokia json output Ramurd Programming 10 08-31-2018 04:16 PM
[SOLVED] Parsing Json with Python slimcharles Programming 6 03-17-2017 09:04 AM
[SOLVED] Parsing Javascript To JSON Using Python 3 cin_ Programming 3 05-18-2015 04:08 PM
[SOLVED] Parsing Javascript To JSON cin_ Programming 5 05-18-2015 04:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration