如何使我的json使用python格式化

时间:2018-07-31 10:43:41

标签: python json stream formatting slurp

我有一个看起来像这样的json:

{
    "message": ".replace(commentRegExp, '')",
    "report_id": 1961272
}{
    "message": ".replace(currDirRegExp, '')",
    "report_id": 1961269
}{
    "message": ".replace(jsSuffixRegExp, '');",
    "report_id": 1961270
}

如何使用python将其设置为正确的格式 我希望json数据看起来像这样:

[
 {
    "message": ".replace(commentRegExp, '')",
    "report_id": 1961272
 },
 {
    "message": ".replace(currDirRegExp, '')",
    "report_id": 1961269
 },
 {
    "message": ".replace(jsSuffixRegExp, '');",
    "report_id": 1961270
 }
]

4 个答案:

答案 0 :(得分:1)

以下是用于读取JSON文本流的通用解决方案。它们不必以换行符分隔。但是,假设在您的路径上。

为说明起见,问题中显示的JSON对象也假定位于名为“ json.txt”的文件中。

import json
import sh

infile='json.txt'
cmd = sh.jq('-M', '-s', '.', infile)
obj = json.loads( cmd.stdout )
print( json.dumps(obj, indent=2) )

这将产生所需的输出。

(要进行测试,您可以运行:jq -s . infile

答案 1 :(得分:0)

以下使用“ pip install jq”模块:https://pypi.org/project/jq/

import json
from jq import jq  # jq(CMD).transform(DATA)

infile='json.txt'

def input(filename):
    with open(filename, 'r') as f:
        return f.read()

str = input( infile ); 

print( jq(".").transform(text=str, multiple_output=True))

输出

以上内容产生:

[{'message': ".replace(commentRegExp, '')", 'report_id': 1961272}, {'message': ".replace(currDirRegExp, '')", 'report_id': 1961269}, {'message': ".replace(jsSuffixRegExp, '');", 'report_id': 1961270}]

JSON输出

产生JSON输出:

print(json.loads(json.dumps(jq(".").transform(text=str, multiple_output=True) )))

答案 2 :(得分:0)

此python3脚本显示了如何仅使用以下两个标头读取文件中的JSON实体流以及如何将它们“浆化”为数组:

import json
from splitstream import splitfile

infile='json.txt'

# Assuming filename contains a stream of JSON texts,
# this function returns each as a Python string 
# that can be read using json.loads(_)
def stream(filename):
    with open(filename, 'r') as f:
        for s in splitfile(f, format="json"):
            yield s

obj = []
for jstr in stream(infile):
    obj += [ json.loads(jstr) ]

print( json.dumps( obj ) )

输出

[{"message": ".replace(commentRegExp, '')", "report_id": 1961272}, {"message": ".replace(currDirRegExp, '')", "report_id": 1961269}, {"message": ".replace(jsSuffixRegExp, '');", "report_id": 1961270}]

格式化输出

$ python3 slurpfile.py | jq .
[
  {
    "message": ".replace(commentRegExp, '')",
    "report_id": 1961272
  },
  {
    "message": ".replace(currDirRegExp, '')",
    "report_id": 1961269
  },
  {
    "message": ".replace(jsSuffixRegExp, '');",
    "report_id": 1961270
  }
]

答案 3 :(得分:-1)

像这样的东西会分解根元素

@font-face {
    font-family: 'fontello';
    src: url('/fonts/fontello.eot');
    src: url('/fonts/fontello.eot') format('embedded-opentype'),
    url('/fonts/fontello.woff') format('woff'),
    url('/fonts/fontello.ttf') format('truetype'),
    url('/fonts/fontello.svg') format('svg');
    font-weight: normal;
    font-style: normal;
}

input[type="password"] {
    font-family: "fontello";
    font-style: normal;
    font-weight: normal;
    speak: none;
    color: red;
    font-size: 16px;

    /* For safety - reset parent styles, that can break glyph codes*/
    font-variant: normal;
    text-transform: none;

    /* Font smoothing. That was taken from TWBS */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    /* Uncomment for 3D effect */
    /* text-shadow: 1px 1px 1px rgba(127, 127, 127, 0.3); */

    /* add spacing to better separate each image */
    letter-spacing: 2px;
}

编辑以从文件读取;

docker start $(docker ps -a -q -f status=exited)

该解决方案的主要工作是基于import json import re json = '{"message":".replace(commentRegExp, '')","report_id":1961272}{"message":".replace(currDirRegExp, '')","report_id":1961269}{"message":".replace(jsSuffixRegExp, '');","report_id":1961270}' match_array = re.findall("[{].*?[}]", json) json_new = "" for x in match_array: json_new+=(x+",") json_new = "["+json_new[:-1]+"]" 正则表达式,它将查找所有json根元素,然后用逗号分隔它们,并在开始和结尾处添加方括号