I have a json file that contains a string of json within an object:
{ "requestType": "POST", "response": { "size": 78, "text": "{\"recordID\":123, \"title\":\"Hello World\", \"content\":\"Lorem ipsum...\"}" } }
I need to interperet the contents of the .response.text
string as json using the json command line interpereter, jq.
When I run this command:
jq '.response.text | @json'
Output: "\"{\\\"recordID\\\":123, \\\"title\\\":\\\"Hello World\\\", \\\"content\\\":\\\"Lorem ipsum...\\\"}\""
I get some weird escaped json string instead of json that I can access via something like this: .response.text | @json | .recordID
.
I realize that the @json
function will take json and output a json escaped string, so there must be another way, but @text
doesn't seem to do anything.
Is there some way to convert a string of escaped json to actual json that I can parse with a command such as this: jq '.response.text | @json | .title'
and get this output: "Hello World"
?
jq is an amazing little command line utility for working with JSON data.
Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);
jq is a lightweight and flexible command-line JSON processor. It is like sed for JSON data – you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text. jq is a fantastic command-line JSON processor.
Use fromjson
.
It parses a string to its appropriate json value. tojson
(and @json
) goes the other way around and takes a json value and converts it to a string.
So you could do this:
.response.text | fromjson.title
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With