21

Here is how I dump a file

with open('es_hosts.json', 'w') as fp:
  json.dump(','.join(host_list.keys()), fp)

The results is

 "a,b,c"

I would like:

 a,b,c

Thanks

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tampa
  • 75,446
  • 119
  • 278
  • 425
  • 4
    What makes you think the output you want is valid JSON? Why do you need it without the quotes? – Martijn Pieters Sep 01 '14 at 20:19
  • I know...but part of a corner case where I dont need the quotes in the output – Tampa Sep 02 '14 at 02:52
  • @MartijnPieters The OP did not suggest the unquoted string would be used directly as JSON. I had a similar situation recently, where the double quotes are added lateron in the process, so I needed the unquoted but JSON-escaped string. – Andreas Maier Nov 08 '16 at 09:21
  • @AndreasMaier: that's a completely different situation. – Martijn Pieters Nov 08 '16 at 09:26
  • @MartijnPieters Thinking about this again, your question seems more appropriate now. Writing a file with extension '.json' seems to indicate that. – Andreas Maier Nov 08 '16 at 09:33
  • 1
    If you want to read a json file and dump part of it to disk (i.e.: save it without the saved file being json), then using `write` may make more sense than using `dump`, e.g. `fp.write(mydata)`. – Fabien Snauwaert Oct 25 '18 at 12:17

5 Answers5

19

Before doing a string replace, you might want to strip the quotation marks:

print '"a,b,c"'.strip('"')

Output:

a,b,c

That's closer to what you want to achieve. Even just removing the first and the last character works: '"a,b,c"'[1:-1].

But have you looked into this question?

Community
  • 1
  • 1
Falko
  • 17,076
  • 13
  • 60
  • 105
  • 1
    This solution is better than the one that suggests to replace all double quotes, but it still removes too much when the last character of the (unquoted) string is an (escaped) double quote, as in `"my name is \"Andy\""`. What you want is to remove exactly only the leading double quote and the trailing double quote. – Andreas Maier Nov 08 '16 at 09:17
  • I should add that my comment applies to the first solution suggested in the answer (using `strip()`). The second solution (using slicing) works for all cases (and would have deserved its own answer so it can be voted up). Still voting this answer up. – Andreas Maier Nov 08 '16 at 09:27
9

To remove the quotation marks in the keys only, which may be important if you are parsing it later (presumably with some tolerant parser or maybe you just pipe it directly into node for bizarre reasons), you could try the following regex.

re.sub(r'(?<!: )"(\S*?)"', '\\1', json_string)

One issue is that this regex expects fields to be seperated key: value and it will fail for key:value. You could make it work for the latter with a minor change, but similarly it won't work for variable amounts of whitespace after :

There may be other edge cases but it will work with outputs of json.dumps, however the results will not be parseable by json. Some more tolerant parsers like yaml might be able to read the results.

import re
regex = r'(?<!: )"(\S*?)"'
o = {"noquotes" : 127, "put quotes here" : "and here", "but_not" : "there"}
s = json.dumps(o)
s2 = json.dumps(o, indent=3)
strip_s = re.sub(regex,'\\1',s)
strip_s2 = re.sub(regex,'\\1',s2)

print(strip_s)
print(strip_s2)

assert(json.loads(strip_s) == json.loads(s) == json.loads(strip_s2) == json.loads(s2) == object)

Will raise a ValueError but prints what you want.

szmoore
  • 924
  • 10
  • 18
1

Use python's built-in string replace function

with open('es_hosts.json', 'w') as fp:
   json.dump(','.join(host_list.keys()).replace('\"',''), fp)
Jeff Sheffield
  • 5,768
  • 3
  • 25
  • 32
1

Well, that's not valid json, so the json module won't help you to write that data. But you can do this:

import json

with open('es_hosts.json', 'w') as fp:
    data = ['a', 'b', 'c']
    fp.write(json.dumps(','.join(data)).replace('"', ''))

That's because you asked for json, but since that's not json, this should suffice:

with open('es_hosts.json', 'w') as fp:
    data = ['a', 'b', 'c']
    fp.write(','.join(data))
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
0

Just use for loop to assign list to string.

import json

with open('json_file') as f:
    data = json.loads(f.read())
    for value_wo_bracket in data['key_name']:
        print(value_wo_bracket)

Note there is difference between json.load and json.loads