10

I want to replace the value in sample json from larger swagger.json file content and it is too large.

Error:
/usr/bin/jq: Argument list too long error bash 

worked to solve this issue for a few days and cannot identify the issue here. this is the sample json file:

{
   "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
   }
}

this is the command i using and it give me a error which i as arguments too large.

swagger = $(cat swagger.json)

jq -r --arg swagger "$swagger" '.apiDefinition = $swagger' <<<"$json"

Can anyone please help!

swagger = $(cat swagger.json)

11
  • Why did you edit out the portion of the script? Can you post it back? and show us exactly how to reproduce the issue? Also post the jq version you are trying this on. Commented Jan 22, 2020 at 7:03
  • i want to replace the apiDefinition keyword values with swagger.json values. Commented Jan 22, 2020 at 7:07
  • jq - commandline JSON processor [version 1.5-1-a5b5cbe] Commented Jan 22, 2020 at 7:07
  • If the file is too long, post it in a external file repo and link it here Commented Jan 22, 2020 at 7:07
  • sorry! cannot share it :( Commented Jan 22, 2020 at 7:08

2 Answers 2

7

The Q does not explicitly say how $swagger has been set, but it would seem that rather than using --arg swagger $swagger you would be better off using one of the file-oriented command-line options, perhaps along the lines of:

--argfile swagger swagger.json

There are many alternatives, but to explore these sensibly here, it would be best if you provided at least one complete but very TINY example. (The example does NOT have to illustrate the "Argument list too long" error!)

Caveat

If you are worried that the --argfile option is deprecated, then by all means use --slurpfile instead if your jq has it, but note that the latter option wraps the file contents into a JSON array, so you would have to take that into account.

These and other options are all presented succinctly in the official documentation at https://stedolan.github.io/jq/manual/

Sign up to request clarification or add additional context in comments.

2 Comments

swagger = $(cat swagger.json)
root@mife-kubernetes-master:/home/svradmin/jsontest# jq -r --slurpfile swagger "$swagger" '.apiDefinition = $swagger' <<<"$json" -bash: /usr/bin/jq: Argument list too long
4

From your example:

jq -r --arg swagger "$swagger" '.apiDefinition = $swagger' <<<"$json"

I'm assuming you want the output to result in a JSON object with a key "apiDefinition", and its value being set to the content of swagger.json (which contains valid JSON).

In that case this works:

jq -n --slurpfile swagger swagger.json '{"apiDefintion": $swagger[0]}'

The first 10 lines of the resulting output:

{
  "apiDefintion": [
    {
      "id": 1,
      "first_name": "Samson",
      "last_name": "Wandrack",
      "email": "[email protected]",
      "gender": "Male",
      "ip_address": "122.171.218.251"
    },
  • -n is required in this case, to use the JSON on the command line as the input JSON, rather than reading it from stdin or a file.
  • --slurpfile puts the contents of swagger.json into the variable $swagger, as an array.
  • -r is not needed, as it has no effect unless the output is just strings (and it's raw output makes it invalid JSON).
  • $swagger[0] is used to only include the first item of the slurped array.

From the documentation at https://stedolan.github.io/jq/manual/:

--null-input/-n:

Don't read any input at all! Instead, the filter is run once using null as the input. This is useful when using jq as a simple calculator or to construct JSON data from scratch.

--slurpfile variable-name filename:

This option reads all the JSON texts in the named file and binds an array of the parsed JSON values to the given global variable. If you run jq with --slurpfile foo bar, then $foo is available in the program and has an array whose elements correspond to the texts in the file named bar.

--raw-output / -r:

With this option, if the filter's result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes. This can be useful for making jq filters talk to non-JSON-based systems.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.