我有一个带有 jsonlines 的文件,想找到空值。
{"name": "Color TV", "price": "1200", "available": ""}
{"name": "DVD player", "price": "200", "color": null}
并想输出空和/或空值及其键:
available: ""
color: null
我认为它应该类似于cat myexample | jq '. | select(. == "")',但不起作用。
我有一个文件,其中每一行都是一个 JSON 对象。我想将文件转换为 JSON 数组。
该文件看起来像这样:
{"address":"email1@foo.bar.com", "topic":"Some topic."}
{"address":"email2@foo.bar.com", "topic":"Another topic."}
{"address":"email3@foo.bar.com", "topic":"Yet another topic."}
我正在使用 bash 和 jq。
我试过
jq --slurp --raw-input 'split("\n")[:-1]' my_file
但这只是将每一行视为一个字符串,创建一个 JSON 字符串数组。
[
  "{\"address\":\"email1@foo.bar.com\", \"topic\":\"Some topic.\"}",
  "{\"address\":\"email2@foo.bar.com\", \"topic\":\"Another topic.\"}",
  "{\"address\":\"email3@foo.bar.com\", \"topic\":\"Yet another topic.\"}"
]
我想要:
[
  {"address":"email1@foo.bar.com", "topic":"Some topic."},
  {"address":"email2@foo.bar.com", "topic":"Another topic."},
  {"address":"email3@foo.bar.com", "topic":"Yet another topic."}
]
有没有办法用 OpenAPI 描述json-lines?
除了似乎还没有 MIME 类型这一事实之外,我想知道是否可以描述这样的响应。
理论上,我的响应可能是一组对象,但我收到了是否可以以 json-lines 形式传递的问题,意思是:只是对象,每行一个。
由于我使用 OpenAPI 来描述我的 API,我对如何描述此响应感到困惑。我可以简单地将响应定义为“字符串”类型,但这对于我的 API 规范的读者来说并不是很有帮助。
这是我的json:
[
 {
  "ReferringUrl": "N",
  "OpenAccess": "0",
  "ItmId": "1694738780"
 },
 {
  "ReferringUrl": "L",
  "OpenAccess": "1",
  "ItmId": "1347809133"
 }
]
我希望它回到原始的json格式:
[{"ReferringUrl": "N","OpenAccess": "0","ItmId": "1694738780"},{"ReferringUrl": "L","OpenAccess": "1","ItmId": "1347809133"}]
如何使用jq来做到这一点?:) 谢谢!
考虑有以下代码和jsonl文件,
我不使用jsonlines.open()api 读取文件是有特定原因的,所以请将此视为事实。
jsonlines 包参考: https://jsonlines.readthedocs.io/en/latest/#jsonlines.Reader
import jsonlines
with open('example.jsonl', 'r') as jsonl_f:
    content = jsonl_f.read()
with jsonlines.Reader(content) as reader:
    lst = [obj for obj in reader]
example.jsonl内容:
{"hello": "world"}
{"covid": "19"}
我上线时出错lst=:
 lst = [obj for obj in reader]
  File "../lib/python3.7/site-packages/jsonlines/jsonlines.py", line 204, in iter
    skip_empty=skip_empty)
  File "../lib/python3.7/site-packages/jsonlines/jsonlines.py", line 164, in read
    six.raise_from(exc, orig_exc)
  File "<string>", line 3, in raise_from
jsonlines.jsonlines.InvalidLineError: line contains invalid json: Expecting property name enclosed in double quotes: …当我安装 npm 包jsonlines时,它被解析为镜像注册表registry.npm.taobao.org而不是registry.npmjs.org. 它仅针对 执行此操作jsonlines。这是什么原因造成的?
这是我的 package-lock.json 的差异。原始的“已解析”值是在另一个开发人员安装该包时创建的:
     "jsonlines": {
       "version": "0.1.1",
-      "resolved": "https://registry.npmjs.org/jsonlines/-/jsonlines-0.1.1.tgz",
+      "resolved": "https://registry.npm.taobao.org/jsonlines/download/jsonlines-0.1.1.tgz",
       "integrity": "sha1-T80kbcXQ44aRkHxEqwAveC0dlMw="
     },
我确认我配置的注册表是 npmjs.org:
$ npm config get registry
https://registry.npmjs.org/
使用用 C# 编写的 ASP.NET Core 6 Web API 和最少的 API,我想返回数据流,而无需先将数据加载到内存中。就我而言,这是由 Apache Spark 编写的 JSONL(JSON 行)数据。JSONL 是一种基于文本的格式。
下面的代码设置了这Content-Type: application/json对于我的用例来说是不正确的。设置此类型然后用数组包装整个内容,并在所有引号的地方添加转义反斜杠字符。
相反,它应该设置Content-type: text/plain保留行的原始格式,并允许该端点的使用者一次流式传输和处理一行,而无需将整个响应正文加载到客户端的内存中。
是否可以content-type在保留流的同时更改此设置Transfer-Encoding: chunked,并且不解析或修改我从 .jsonl 文件中读取的行内容?
app.MapGet("/stream/data", () =>
{
    async IAsyncEnumerable<string> Stream()
    {
        using (StreamReader file = new StreamReader(filePath))
        {
            while (!file.EndOfStream)
            {
                yield return await file.ReadLineAsync() ?? string.Empty;
            }
        }
    }
    return Stream();
});
使用以下命令可以读取 json 文件:
library(jsonlite)
json_text <- readLines("tect.json", warn = FALSE, encoding = "UTF-8")
json_data <- fromJSON(txt = paste(json_text, collapse = ""))
tect.json 文件中的数据格式如下:
[
  {
    "id": 1,
    "name": "Apple",
    "color": "red",
    "price": 0.99
  },
  {
    "id": 2,
    "name": "Banana",
    "color": "yellow",
    "price": 0.5
  },
  {
    "id": 3,
    "name": "Orange",
    "color": "orange",
    "price": 0.75
  }
]
但是,如果 tech.json 具有这种格式,如何将其作为 json 文件读取并将其转换为数据帧?
{"id": 1, "name": "Apple", "color": "red", "price": 0.99 }
{"id": 2, "name": "Banana", "color": "yellow", "price": 0.5 …我有一个JSON文件,我正在读取节点,修改并将其保存为json文件.
我希望保存新的json作为换行分隔vs在数组中.
我遇到了https://github.com/CrowdProcess/newline-json,但没有完全理解流.如果我有以下流设置,我如何通过解析器和字符串管道它?
fileStream = fs.createReadStream('source.json')
writeStream = fs.createWriteStream('output.txt');
var Parser = require('newline-json').Parser;
var Stringifier = require('newline-json').Stringifier;
var parser = new Parser();
var stringifier = new Stringifier();
但是运行以下命令只会输出一个空白文件.
fileStream.pipe(parser).pipe(stringifier).pipe(writeStream)
我对溪流有什么看法?
我很难尝试将import.io中的API响应加载到文件或列表中.
我正在使用的是 https://data.import.io/extractor/{0}/json/latest?_apikey={1}
以前我的所有脚本都设置为使用普通的JSON,并且一切都运行良好,但现在嘿已经决定使用json线,但不知怎的,它似乎格格不入.
我尝试调整脚本的方法是以下列方式读取API响应:
url_call = 'https://data.import.io/extractor/{0}/json/latest?_apikey={1}'.format(extractors_row_dict['id'], auth_key)
r = requests.get(url_call)
with open(temporary_json_file_path, 'w') as outfile:
    json.dump(r.content, outfile)
data = []
with open(temporary_json_file_path) as f:
    for line in f:
        data.append(json.loads(line))
这样做的问题是,当我检查数据[0]时,所有的json文件内容都被转储到其中......
data[1] = IndexError: list index out of range
这是一个例子data[0][:300]:
u'{"url":"https://www.example.com/de/shop?condition[0]=new&page=1&lc=DE&l=de","result":{"extractorData":{"url":"https://www.example.com/de/shop?condition[0]=new&page=1&lc=DE&l=de","resourceId":"23455234","data":[{"group":[{"Brand":[{"text":"Brand","href":"https://www.example.com'
有没有人对此API的响应有经验?我从其他来源做的所有其他jsonline读取工作正常,除了这一个.
根据评论编辑:
print repr(open(temporary_json_file_path).read(300))
给出这个:
'"{\\"url\\":\\"https://www.example.com/de/shop?condition[0]=new&page=1&lc=DE&l=de\\",\\"result\\":{\\"extractorData\\":{\\"url\\":\\"https://www.example.com/de/shop?condition[0]=new&page=1&lc=DE&l=de\\",\\"resourceId\\":\\"df8de15cede2e96fce5fe7e77180e848\\",\\"data\\":[{\\"group\\":[{\\"Brand\\":[{\\"text\\":\\"Bra'
我有这样的json:
[ {"one": 1}, {"two": 2}]
并希望将其转换为以下格式:
{"one": 1}
{"two": 2}
以便将其索引到ElasticSearch中。(后一种称为“ jsonl”格式)。JQ是我的首选工具,但是我不知道该怎么做。谢谢
给定一个返回 a 的 API jsonl,我如何操作我获得的数据?
如果 API 给我这样的数据怎么办:
{"plate": "pizza", "quantity": 3}
{"plate": "pasta", "quantity": 2}
{"plate": "pizza", "quantity": 3}
{"plate": "pasta", "quantity": 2}
{"plate": "hotdog", "quantity": 7}
我怎样才能做到维护类型.jsonl而不是创建数组?
非常感谢您的帮助
虽然此代码读取和写入 jsonlines 文件。怎么压缩呢?我尝试直接使用gzip.open,但出现各种错误。
import json
    
def dump_jsonl(data, output_path, append=False):
    """
    Write list of objects to a JSON lines file.
    """
    mode = 'a+' if append else 'w'
    with open(output_path, mode, encoding='utf-8') as f:
        for line in data:
            json_record = json.dumps(line, ensure_ascii=False)
            f.write(json_record + '\n')
    print('Wrote {} records to {}'.format(len(data), output_path))
def load_jsonl(input_path) -> list:
    """
    Read list of objects from a JSON lines file.
    """
    data = []
    with open(input_path, 'r', encoding='utf-8') as f:
        for line in f: …