Previously:

  • Using JQ to process JSON data
  • Using JQ to process JSON data

Today, I’m going to share the last part of the JQ tool: file format conversion.

Jq formulas are simple conversions from JSON to CSV.

Extract the data

We will convert the article array of the Funtester. json file to a CSV file.

First we filter out the article content through pipe characters.

Fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq '. The article [] '{" author ":" tester1 ", "title" : "ApiTest" } { "author": "tester2", "title": "performanceTest" }Copy the code

Here we get a set of JSON data instead of using. Artworks (without []), in which case we get an array as follows:

Fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq. 'article' [{" author ":" tester1 ", "title" : "ApiTest" }, { "author": "tester2", "title": "performanceTest" } ]Copy the code

[] is a set of objects, without an array of objects.

The assembly data

Next, you need to convert these JSON objects into arrays. Here we use the syntax we learned from combining pipe characters and functions: add a pipe character for each JSON object data.

✘ fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq '. The article [] | [, author, title] '[" tester1." "ApiTest" ] [ "tester2", "performanceTest" ]Copy the code

The new filter [.author,.title] processes the returned JSON data. The key is the author and title values in the JSON data, and is joined with a comma.

The output document

As a final step, we can apply the @csv operator, which formats the JSON array as a CSV line:

Fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq '. The article [] | [, author, title] | @ CSV 'tester1' \ '\ ", \ "ApiTest \" " "\"tester2\",\"performanceTest\""Copy the code

You can see here that the semicolon inside is escaped because JQ applies JSON encoding to its output by default.

So to get the raw CSV output, we need to add the -r parameter:

Fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq -r '. The article [] | [, author, title] | @ CSV '" tester1 ", "ApiTest" "tester2","performanceTest"Copy the code

FunTester.Tencent Cloud Author of the Year,Boss direct hire contract author.Official GDevOps media partner, non-famous test development.

  • FunTester test framework architecture diagram
  • FunTester test project architecture diagram
  • Selenium4 IDE features: Elastic testing, looping, and logical judgment
  • Groovy uses topics in JMeter
  • Thread-safe classes are used in performance testing
  • Manual testing or automated testing?
  • Summary of Automatic Test failures on the Web
  • Socket interface asynchronous authentication practice
  • Use ThreadLocal to solve thread synchronization problems
  • Replay browser multiple request performance testing practices