Question: I have a JSON object that contains strings of JSON at various levels in the JSON such as the one below that I want converted to as nested JSON.

Input:

```
{
    "requestType": "POST",
    "response": {
        "size": 2,
        "text": "{\"recordID\":1, \"title\":\"Hello\", \"content\":\"Lorem ipsum\"}"
    },
    "scheme": "{\"scheme\":\"https\"}"
}

```

Output:

```
{
    "requestType":"POST",
    "response":{
        "size":2,
        "text":{
            "recordID":1,
            "title":"Hello",
            "content":"Lorem ipsum"
        }
    },
    "scheme":{
        "scheme":"https"
    }
}

```

I know it's possible to convert one specific object using `fromjson` but is there an easier way to convert all occurrances of these string to json?

Answer: 
If your jq has `walk` then you can simply write:

```
walk(if type=="string" then fromjson? // . else . end)

```

Otherwise, you can copy-and-paste the definition of `walk/1`, which can readily be found by googling `jq def walk`.

Roll your own
-------------

Alternatively:

```
def dej:
  if type == "string" then fromjson? // . 
  elif type == "object" then map_values(dej)
  elif type == "array" then map(dej)
  else .
  end;

dej

```

Deep de-jsonification
---------------------

If you want `dej` as defined above to be applied as many times as required to achieve quiescence, then you could use this definition:

```
def dejx:
  def dej:
    if type == "string" then fromjson? // . 
    elif type == "object" then map_values(dej)
    elif type == "array" then map(dej)
    else .
    end;
  dej as $d | if . == $d then . else $d|dejx end;

```

Using `fromjson` directly
-------------------------

For simple cases, you can just use `fromjson`, e.g. in your case:

```
.scheme |= fromjson
| .response.text |= fromjson

```
Bash is not a good language for handling JSON data, best is to use a higher level language. Here is a sample implementation in Ruby 2.4 (for the `transform_values` method):

```
#!/usr/bin/env ruby                                                             
require 'json'                                                                  

def parse(v)                                                                    
  v = JSON.parse(v) rescue v                                                    
  if v.respond_to? :transform_values                                            
    v.transform_values {|w| parse(w)}                                           
  elsif v.respond_to? :map                                                      
    v.map{|w| parse(w)}                                                         
  else                                                                          
    v                                                                           
  end                                                                           
end                                                                             

puts JSON.pretty_generate parse(ARGF.read)

```

Here is a sample run on a modified example, that includes arrays:

```
$ cat file
{
    "requestType": [ "{\"a\": \"b\"}", 123 ],
    "response": {
        "size": 2,
        "text": "{\"recordID\":1, \"title\":\"Hello\", \"content\":\"Lorem ipsum\"}",
        "arr": "[1,2,3]"
    },
    "scheme": "{\"scheme\":\"https\"}"
}

$ ./test.rb file
{
  "requestType": [
    {
      "a": "b"
    },
    123
  ],
  "response": {
    "size": 2,
    "text": {
      "recordID": 1,
      "title": "Hello",
      "content": "Lorem ipsum"
    },
    "arr": [
      1,
      2,
      3
    ]
  },
  "scheme": {
    "scheme": "https"
  }
}

```