Title: Merging a long JSON with another using jq
Tags: json;bash;jq
Question: I need to do some CURLs from which I'm building one output JSON. This is how I managed to perform a merge using a function:

```...
ADDITIONALJSONDATA="{\"$DATATYPE\" : "$DATA"}" 
MERGEDENTRY=$(echo $SOURCE | jq --argjson json "$ADDITIONALJSONDATA" '. += $json' | tr -d '\r\n')
 ...
```

It seems when JSON inside ```$DATA``` is big enough then I'm getting ```Argument list too long``` error. Is it possible (in a nice way) to treat JSON to merge as a single argument here?

Comment: As an aside -- all-caps variable names are in a reserved namespace; see http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html for POSIX guidelines, specifying that all-caps names are used for shell and environment variable names with meaning to the shell and POSIX-defined utilities, whereas lowercase names are reserved for applications and guaranteed not to conflict.

Comment: Also, much better to use `jq -c` to tell jq not to put extra whitespace in to begin with rather than trying to use `tr` to take it out after-the-fact. You might also want the `-j` argument to remove trailing whitespace, though this isn't always pertinent in a command substitution since those remove a trailing newline if one exists anyhow.


Here is another answer: You could use process substitution and ```--slurpfile``` option to solve your issue:

```MERGEDENTRY=$(echo "$SOURCE" | jq --slurpfile json <(printf '%s\n' "$ADDITIONALJSONDATA") '. += $json[0]' | tr -d '\r\n')
```

As per Charles' suggestion, we can simplify further by using ```<<<``` instead of ```echo "$SOURCE" | ...```:

```MERGEDENTRY=$(jq --slurpfile json <(printf '%s\n' "$ADDITIONALJSONDATA") '. += $json[0]' <<< "$SOURCE" | tr -d '\r\n')
```


Here is another answer: 
If, as seems to be the case here, you already have $DATA and $DATATYPE, there is no need for ADDITIONALJSONDATA
In general, using ```tr -d '\r\n'``` here is very bad practice, e.g. because it could scrunch 1 and 2 together to make 12


Hopefully the following will meet your requirements:

```MERGEDENTRY=$(jq -c --arg TYPE "$DATATYPE" --slurpfile A <(printf '%s\n' "$DATA") '
  . += {($TYPE): $A[0]}' <<< "$SOURCE")
```

(We need to use ```$A[0]``` here because "slurping" puts $DATA into an array.) 

Comment for this answer: Can you test whether process substitution is working without using jq? Have you tried putting $DATA into a file so you can check the jq command without using process substitution?
Comment for this answer: I'm getting `Bad JSON in --slurpfile A /proc/5992/fd/63: Could not open /proc/5992/fd/63: No such file or directory` Can MINGW64 and WinOS be a reason for that?

This is the SOURCE:
`{
 "id": "devlocal",
 "name": "devlocal",
...}`

This is the DATA:
`[{
  "username": "52352",
  "enabled": true,
  },
..]`

Here is another answer: I managed to do it far easier:
```MERGEDENTRY=${sour181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16-1}","\"$data_type\"":"$data","```
And then just keep in mind we need to end JSON payload properly by replacing ```'``` by ```}```.

I can do this as I'm always adding new JSON payload at the end of existing one.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:11/22/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<json><bash><jq>', 'Title': 'Merging a long JSON with another using jq', 'ViewCount': 631, 'id': 47442344, 'user_AboutMe': '<p>Student of Control Engineering and Robotics at Poznan University of Technology. Interested in Java and Swift programming.</p>\n', 'user_AccountId': 5308742.0, 'user_DisplayName': 'Maxsimus', 'user_Reputation': 47.0, 'user_Views': 9.0, 'user_id': 4235798.0}}