3

I have a python script providing command line / output in console on remote linux. I have another script which is reading this output on local machine. Output is in below format:

ABC: NEG
BCD: NEG
FGH: POS
{aa:bb:cc:dd:ee{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}}

notice last line is in json format, now I want to check which line is in json format of the output. I tried

if "value" in line:
    json.loads(line)

it is not reading and even

json.dumps(line)

not giving output ?

1 Answer 1

7

You can use try except clause to check if a string is actually json:

import json
line = '<what you think is json>'
try:
    json_line = json.loads(line)
except ValueError:
    print("not a json")

In your above code the last line is not a valid JSON. You can use this tool JSONLint to verify if your JSON is a valid JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I tried it. It is saying no json object could be detected.
@Error_Coding Which is because your last line is not a valid JSON. Only this {"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true} segment in the last line is a valid JSON

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.