6

I have a JSON file that looks like this:

{"environment": "production", 
 "classes": 
    {"nfs::server": {"exports": ["/srv/share1","/srv/share3"]}}
}

When I run the following code using Python 3.6

fp=open('example.json', 'r')
data=json.load(fp)

print(50*'-')
print(json.dumps(data, indent=4))
print(50*'-')
json.dump(data, sys.stdout, indent=4)

I get the output:

--------------------------------------------------
{
    "environment": "production",
    "classes": {
        "nfs::server": {
            "exports": [
                "/srv/share1",
                "/srv/share3"
            ]
        }
    }
}
--------------------------------------------------
{
    "environment": "production",
    "classes": {
        "nfs::server": {
            "exports": [
                "/srv/share1",
                "/srv/share3"
            ]
        }
    }
}%

My question is why is the extra % included in the json.dump output compared to the json.dumps string? It is not an artifact of the OS because if I provide a file object instead of sys.stdout it also gets written to the file.

2
  • Could this be an artifact of an encoding mismatch? Commented Mar 24, 2017 at 14:36
  • Issue observable only using zsh, not using bash Commented Mar 24, 2017 at 14:59

2 Answers 2

5

The last % is the first character of your console prompt line or a feature of your shell (https://unix.stackexchange.com/questions/167582/why-zsh-ends-a-line-with-a-highlighted-percent-symbol)

Nothing to do with json, nor python.

Because, when print() add a '\n' at the end, the dump to stdout doesn't

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

8 Comments

it does not :) sys.stdout is not a file, it's the standard output (ie. console). What the OP copy-pasted may be what was displayed in his console... hence the spurious % char
because, if the OP does a cat file.txt, the result is the same, with not trailing \n, he see the next % of the prompt :) (just a guess)
you are right, me neither, but when I forget the trailing \n I still get a %: echo -n tutu tutu%... It may still be due to the shell behavior and not python
I think it is a shell thing. Although I have no % in my shell prompt (using zsh), if I switch to bash this problem disappears. Thanks for the help guys.
I just had the same thing happen using zsh. Adding a newline fixed it. Good to know.
|
4

The reason is that print(json.dumps(data, indent=4)) prints a newline, and json.dump(data, sys.stdout, indent=4) does not.

You can try adding a print() at the end:

print(50*'-')
print(json.dumps(data, indent=4))
print(50*'-')
json.dump(data, sys.stdout, indent=4)
print()

Is the % symbol part of your shell's prompt?

2 Comments

While this solves the problem of output to the console, it does not solve output to a file, ie. replace sys.stdout with a file object. My shell prompt has no % but it is still output to a file when using json.dump
It's ultimately a shell issue, using zsh I observe the trailing % but with bash no trailing %. Thanks for the help guys.

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.