How to Explicitly Set Carriage Return When Doing JSON.Dump

How to explicitly set carriage return when doing json.dump?

If you insist on consistent CRLF behavior (the JSON spec requires parsers to handle both, but opening it in certain plain text readers like Notepad might be easier with consistent CRLF), the solution is in the open function, not the json module.

Just pass newline='\r\n' to open, and it will translate any \n written by json to \r\n seamlessly on all systems, rather than the default behavior of translating to os.linesep (which is \r\n on Windows and \n on most other OSes):

with open(fpath, 'w', encoding="utf-8", newline='\r\n') as outfile:
json.dump(data, outfile, indent=4, sort_keys=True, ensure_ascii=False)

Include carriage return correctly in iPython notebooks when writing using json.dump

Markdown uses newlines to separate lines, not HTML <br/> tags. Include newlines in your source lines; use double newlines to separate paragraph elements (including headers):

cell = {
"cell_type": "markdown",
"metadata": {'collapsed': False, 'name': 'test'},
"source": [
"## Header line\n\n",
"Second line, not a header...hopefully"
],
}

Add a new role to a Chef .json file with Sed

I don't know if this is the best approach (vs sed, AWK, or Perl) but it is straightforward to do what you're asking using python's json library.

import json

# read the file as a dict using json.loads
d = json.loads(open('servername.json', 'r').read())

# add your new role to the end of the run_list
d['run_list'].append('role[My_New_Role]')

# write new json to file (specify a new file, or overwrite if you prefer)
open('new_servername.json', 'w').write(json.dumps(d, indent=2))

The output file looks like:

{
"chef_environment": "test",
"name": "myserver123",
"run_list": [
"role[base-pkg]",
"role[interesting_stuff]",
"role[user_apps]",
"role[My_New_Role]"
]
}

It's pretty easy to modify this code into a script with the filename as an input so that it's easy to run multiple times.

Removing a new line feed in json file using Python.

Reading between the lines, I think the input format might be a single JSON array, and the desired output is newline-separated JSON representations of the elements of that array. If so, this is probably all that's needed:

with open('testnoline.json', 'w') as outfile:
for obj in data_json:
outfile.write(json.dumps(obj) + "\n")

How to remove whitespaces and newlines from every value in a JSON file?

Now I want to strip off all he whitespaces and newlines for every value in the JSON file

Using pkgutil.simplegeneric() to create a helper function get_items():

import json
import sys
from pkgutil import simplegeneric

@simplegeneric
def get_items(obj):
while False: # no items, a scalar object
yield None

@get_items.register(dict)
def _(obj):
return obj.items() # json object. Edit: iteritems() was removed in Python 3

@get_items.register(list)
def _(obj):
return enumerate(obj) # json array

def strip_whitespace(json_data):
for key, value in get_items(json_data):
if hasattr(value, 'strip'): # json string
json_data[key] = value.strip()
else:
strip_whitespace(value) # recursive call

data = json.load(sys.stdin) # read json data from standard input
strip_whitespace(data)
json.dump(data, sys.stdout, indent=2)

Note: functools.singledispatch() function (Python 3.4+) would allow to use collections' MutableMapping/MutableSequence instead of dict/list here.

Output

{
"anotherName": [
{
"anArray": [
{
"anotherKey": "value",
"key": "value"
},
{
"anotherKey": "value",
"key": "value"
}
]
}
],
"name": [
{
"someKey": "some Value"
},
{
"someKey": "another value"
}
]
}

Python: Handling newlines in json.load() vs json.loads()

json.load() reads from a file descriptor and json.loads() reads from a string.

Within your file, the \n is properly encoded as a newline character and does not appear in the string as two characters, but as the correct blank character you know.

But within a string, if you don't double escape the \\n then the loader thinks it is a control character. But newline is not a control sequence for JSON (newline is in fact a character like any other).

By doubling the backslash you actually get a real string with \n in it, and only then will Python transform the \n into a newline char.

How to remove ^M

If you're writing the file, you should specify open(filename, "wb"). That way, you'll be writing in binary mode, and Python won't attempt to determine the correct newlines for the system you're on.



Related Topics



Leave a reply



Submit