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

后端 未结 2 1455
旧巷少年郎
旧巷少年郎 2021-01-07 05:38

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 
         


        
2条回答
  •  青春惊慌失措
    2021-01-07 06:13

    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": "swandrack0@hibu.com",
          "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.

提交回复
热议问题