我想从较大的swagger.json文件内容中替换示例json中的值,并且该值太大。
Error:
/usr/bin/jq: Argument list too long error bash
工作了几天来解决了这个问题,无法在此处找到问题。 这是示例json文件:
{
"name": "",
"description": "",
"context": "",
"version": "",
"provider": "cbs",
"apiDefinition": "",
"wsdlUri": null,
"responseCaching": "Disabled",
"cacheTimeout": 300,
"destinationStatsEnabled": false,
"isDefaultVersion": true,
"transport": [
"http",
"https"
],
"tags": ["PROVIDER_","MIFE"],
"tiers": ["Unlimited","Default","Silver","Subscription","Gold","Premium","Bronze"],
"maxTps": {
"sandbox": 5000,
"production": 1000
},
"visibility": "PUBLIC",
"visibleRoles": [],
"endpointConfig": "",
"endpointSecurity": {
"username": "user",
"type": "basic",
"password": "pass"
},
"gatewayEnvironments": "Production and Sandbox",
"sequences": [],
"subscriptionAvailability": null,
"subscriptionAvailableTenants": [],
"businessInformation": {
"businessOwnerEmail": "BUSINESSOWNEREMAIL_",
"technicalOwnerEmail": "TECHNICALOWNEREMAIL_",
"technicalOwner": "TECHNICALOWNER_",
"businessOwner": "BUSINESSOWNER_"
},
"corsConfiguration": {
"accessControlAllowOrigins": ["*"],
"accessControlAllowHeaders": [
"authorization",
"Access-Control-Allow-Origin",
"Content-Type",
"SOAPAction"
],
"accessControlAllowMethods": [
"GET",
"PUT",
"POST",
"DELETE",
"PATCH",
"OPTIONS"
],
"accessControlAllowCredentials": false,
"corsConfigurationEnabled": false
}
}
这是我使用的命令,它给我一个错误,我作为参数太大。
swagger = $(cat swagger.json)
jq -r --arg swagger "$swagger" '.apiDefinition = $swagger' <<<"$json"
任何人都可以帮忙!
swagger = $(cat swagger.json)
答案 0 :(得分:3)
Q并没有明确说明如何设置$swagger
,但似乎最好使用一种面向文件的命令行选项,而不是使用--arg swagger $swagger
遵循以下原则:
--argfile swagger swagger.json
有很多选择,但是如果要在这里明智地探索它们,最好提供至少一个完整但非常细腻的例子。 (该示例不必说明“ 参数列表太长”错误!)
如果您担心不赞成使用--argfile
选项,那么如果您的--slurpfile
拥有jq
,请务必使用import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
const diagnosticsCollection: vscode.DiagnosticCollection = vscode.languages.createDiagnosticCollection(
'test'
);
const diagnosticsCollection2: vscode.DiagnosticCollection = vscode.languages.createDiagnosticCollection(
'test2'
);
vscode.workspace.onDidChangeTextDocument((event: vscode.TextDocumentChangeEvent) => {
const document = event.document;
const diagnostics: vscode.Diagnostic[] = [];
for (let i = 0; i < document.lineCount; i++) {
const line = document.lineAt(i);
const index = line.text.indexOf('hello');
if (index >= 0) {
diagnostics.push(
new vscode.Diagnostic(
new vscode.Range(i, index, i, index),
'The word hello',
vscode.DiagnosticSeverity.Warning,
)
);
}
}
diagnosticsCollection.set(document.uri, diagnostics);
});
vscode.workspace.onDidSaveTextDocument((document: vscode.TextDocument) => {
const diagnostics: vscode.Diagnostic[] = [];
for (let i = 0; i < document.lineCount; i++) {
const line = document.lineAt(i);
const index = line.text.indexOf('world');
if (index >= 0) {
diagnostics.push(
new vscode.Diagnostic(
new vscode.Range(i, index, i, index),
'The word world',
vscode.DiagnosticSeverity.Error,
)
);
}
}
diagnosticsCollection2.set(document.uri, diagnostics);
});
}
,但是请注意,后一个选项会将文件内容包装到JSON数组,因此您必须考虑到这一点。
这些选项和其他选项均在https://stedolan.github.io/jq/manual/的官方文档中简要介绍了
答案 1 :(得分:2)
根据您的示例:
jq -r --arg swagger "$swagger" '.apiDefinition = $swagger' <<<"$json"
我假设您希望输出结果导致键为“ apiDefinition”的JSON对象,并且其值设置为swagger.json的内容(包含有效JSON)。
在这种情况下,它起作用:
jq -n --slurpfile swagger swagger.json '{"apiDefintion": $swagger[0]}'
结果输出的前10行:
{
"apiDefintion": [
{
"id": 1,
"first_name": "Samson",
"last_name": "Wandrack",
"email": "swandrack0@hibu.com",
"gender": "Male",
"ip_address": "122.171.218.251"
},
摘自https://stedolan.github.io/jq/manual/上的文档:
-空输入/ -n:
根本不读任何输入!而是使用null作为输入运行一次过滤器。当将jq用作简单的计算器或从头开始构建JSON数据时,这很有用。
-slurpfile变量名文件名:
此选项读取命名文件中的所有JSON文本,并将已解析的JSON值数组绑定到给定的全局变量。如果使用--slurpfile foo bar运行jq,则$ foo在程序中可用,并且具有一个数组,该数组的元素与名为bar的文件中的文本相对应。
-原始输出/ -r:
使用此选项,如果过滤器的结果是字符串,则将其直接写入标准输出,而不是将其格式化为带引号的JSON字符串。这对于使jq过滤器与基于非JSON的系统进行通信很有用。