Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重新格式化json,如下所示_Python_Json - Fatal编程技术网

Python 重新格式化json,如下所示

Python 重新格式化json,如下所示,python,json,Python,Json,我从我的请求中获得了以下数据体,这些数据将被插入到我的elasticsearch数据库中。现在看起来是这样的 { "index": { "_index": "menu", "_type": "_doc" } } { "input": "burger", "output": { "category": "Sides", "item": "Angus Burger", "modifiers

我从我的请求中获得了以下数据体,这些数据将被插入到我的
elasticsearch
数据库中。现在看起来是这样的

{
    "index": {
        "_index": "menu",
        "_type": "_doc"
    }
}
{
    "input": "burger",
    "output": {
        "category": "Sides",
        "item": "Angus Burger",
        "modifiers": [],
        "quantity": 1
    }
}
我想解析它,让它看起来像这样

{"index": {"_index": "menu", "_type": "_doc"} }
{"input": "burger","output": {"category": "Sides","item":"Angust Burger","modifiers": [],"quantity": 1} }
我知道一定有一种方法可以使用
python
来实现这一点,但我不知道如何开始。任何帮助都将不胜感激

更新1:

在尝试了以下步骤之后

as_a_string = {
    "index": {
        "_index": "menu_trial_2_no_mods",
        "_type": "_doc"
    }
}
{
    "input": "small mac-n-cheese",
    "output": {
        "category": "Sides",
        "item": "Chick-fil-A Mac-n-Cheese",
        "modifiers": [],
        "quantity": 1
    }
}

val = json.dumps(json.loads(as_a_string))
我得到以下错误

TypeError: the JSON object must be str, bytes or bytearray, not dict
更新2:

在执行
json.dumps()之后,它将以以下格式显示

但我在找这个

{"index": {"_index": "menu_trial_2_no_mods", "_type": "_doc"} }
{"input": "small mac-n-cheese","output": {"category": "Sides","item":"Chick-fil-A Mac-n-Cheese","modifiers": [],"quantity": 1} }

问题中的数据是两个散列,要获得JSON,可以先将它们放在某种iterable中,然后对每个项目执行
JSON.dump

data = [
{
    "index": {
        "_index": "menu",
        "_type": "_doc"
    }
},
{
    "input": "burger",
    "output": {
        "category": "Sides",
        "item": "Angus Burger",
        "modifiers": [],
        "quantity": 1
    }
}
]

import json
for datum in data:
    json.dumps(datum)
结果是:

{"index": {"_index": "menu", "_type": "_doc"}}
{"input": "burger", "output": {"category": "Sides", "item": "Angus Burger", "modifiers": [], "quantity": 1}}

问题中的数据是两个散列,要获得JSON,可以先将它们放在某种iterable中,然后对每个项目执行
JSON.dump

data = [
{
    "index": {
        "_index": "menu",
        "_type": "_doc"
    }
},
{
    "input": "burger",
    "output": {
        "category": "Sides",
        "item": "Angus Burger",
        "modifiers": [],
        "quantity": 1
    }
}
]

import json
for datum in data:
    json.dumps(datum)
结果是:

{"index": {"_index": "menu", "_type": "_doc"}}
{"input": "burger", "output": {"category": "Sides", "item": "Angus Burger", "modifiers": [], "quantity": 1}}

您不能以这种方式将JSON传递给python,python只会在分配一个字典时看到这一点

json1={
“索引”:{
“\u索引”:“菜单\u试验\u 2\u编号\u模块”,
“\u类型”:“\u单据”
}
}
json2={
“输入”:“小型mac-n-cheese”,
“产出”:{
“类别”:“侧面”,
“项目”:“Chick-fil-A Mac-n-Cheese”,
“修饰语”:[],
“数量”:1
}
}
输出

>> json1
{'index': {'_index': 'menu_trial_2_no_mods', '_type': '_doc'}}

>> json2
{'input': 'small mac-n-cheese',
 'output': {'category': 'Sides',
  'item': 'Chick-fil-A Mac-n-Cheese',
  'modifiers': [],
  'quantity': 1}}

您不能以这种方式将JSON传递给python,python只会在分配一个字典时看到这一点

json1={
“索引”:{
“\u索引”:“菜单\u试验\u 2\u编号\u模块”,
“\u类型”:“\u单据”
}
}
json2={
“输入”:“小型mac-n-cheese”,
“产出”:{
“类别”:“侧面”,
“项目”:“Chick-fil-A Mac-n-Cheese”,
“修饰语”:[],
“数量”:1
}
}
输出

>> json1
{'index': {'_index': 'menu_trial_2_no_mods', '_type': '_doc'}}

>> json2
{'input': 'small mac-n-cheese',
 'output': {'category': 'Sides',
  'item': 'Chick-fil-A Mac-n-Cheese',
  'modifiers': [],
  'quantity': 1}}

检查我的最新答案。我以为您的数据是字符串,但看到的是dict(),请尝试使用
json.dumps
编辑答案。您必须将所需的每个对象转换为json。请检查我的更新答案。我以为您的数据是字符串,但看到的是dict(),请尝试使用
json.dumps
编辑答案。您必须将所需的每个对象转换为json。