How to convert JSON subelement strings to real JSON elements in bash How to convert JSON subelement strings to real JSON elements in bash elasticsearch elasticsearch

How to convert JSON subelement strings to real JSON elements in bash


To convert the .data element from a JSON string into a JSON object, you could use the filter:

.data |= fromjson

If you just want to extract the .data element and convert it, you could use the filter:

.data | fromjson

For example: jq -c '.data|fromjson' data.json

Supplemental question

If it is uncertain whether or not fromjson will succeed, you could use the idiom: fromjson? // ., e.g.:

.data |= (fromjson? // .)


While this is valid JSON, you probably need to parse the data element of the object.

If you would accept python:

  1. write to a file, say record.json (for convenience, you can as well use stdin)
  2. python (replace file names or use sys.stdin and sys.stdout)

    import jsonrecord = json.load(open('/path/to/record.json'))data = json.loads(record['data'])record['data'] = datajson.dump(record, open("/path/to/result.json", "w"))