Question: I have a json file with the following format:

```
{userid: 1, client_visited: [1001, 1002]}
{userid: 2, client_visited: [1001, 1004]}
{userid: 3, client_visited: [1004, 1006]}
{userid: 4, client_visited: [1003, 1005, 1006]}
{userid: 5, client_visited: [1006]}

```

I have a mapping of clients that is output from MYSQL db:

```
{"1001": a, "1002": b, "1003": c, "1004": d, "1005": e, "1006": f}

```

How can I update the above json file for the following output?

```
{userid: 1, client_visited: ['a', 'b']}
{userid: 2, client_visited: ['a', 'd']}
{userid: 3, client_visited: ['d', 'f']}
{userid: 4, client_visited: ['c', 'e', 'f']}
{userid: 5, client_visited: ['f']}

```

I am looking at `jq` command to achieve this in a shell script.

Answer: 
First, jq is a JSON processor, so you will have to convert your file and dictionary to valid JSON. (For some guidance in doing so, see the [jq FAQ](https://github.com/stedolan/jq/wiki/FAQ "jq FAQ")). 

Second, you haven't indicated where the dictionary is to be found. In the following, I'll assume it can be included in the program file but there are many other options (see e.g. the manual regarding --argjson).

Third, there are various ways to specify a program. I'll assume you have the following in a file named program.jq:

```
def dict: {"1001": "a", "1002": "b", "1003": "c", "1004": "d", "1005": 
"e", "1006": "f"};

.client_visited |= map(dict[tostring])

```

The invocation: jq -f program.jq input.json

yields the desired result (but of course it will be valid JSON).

Postscript
----------

In the comments, the OP asked about using a distinguished value rather than null should the dictionary be missing a lookup value. To do so, consider:

```
.client_visited |= map(tostring as $s
                       | if dict|has($s) then dict[$s] else 0 end)

```

The following might also be worth considering:

```
.client_visited |= map(dict[tostring] // 0)

```
Not as succinct as [peak](https://stackoverflow.com/users/997358/peak)'s answer but here is a solution using **reduce** to iterate over the indices of `client_visited` updating each element. 

```
reduce (.client_visited|range(length)) as $i (
    .
  ; .client_visited[$i] |= $arg[tostring]
)

```

To specify `$arg` you can use `--argfile` and invoke with something like

```
jq --argfile arg mapping.json -f filter.jq data.json

```