In the previous article working with JSON data using JQ (1), I shared the basic usage of the JQ tool. Today we start sharing high-level uses of JQ, including pipe characters, functions, and format conversions.

Pipe characters and functions

In this section, YOU’ll share more of JQ’s methods for filtering JSON data.

Using the | operator, we can combine two filters. It works like a Unix pipe character. Output from the filter on the left is passed to the filter on the right.

. Please note that the name first. The name |. The first use of the results are the same, will be JSON data. The name node data is passed to the second filter, and then select. First.

Pipes can be combined with other functions. For example, we can use the keys function to get the set of keys for a node in JSON data:

✘ fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq. '| keys' [" article ", "name"] fv @ FunTester  ~ / Downloads Copy the code

Using the length function, we can get the length of the number of elements in the array:

Fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq. '| keys | length' 2Copy the code

The output of the length function depends on the input element:

  • If a string is passed, the character is returnedlength
  • For arrays, return the number of elementslengthorsize
  • For objects, return key-value pairssize

We can also combine the length function with the operator:

Fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq. '| keys | length > 1' trueCopy the code

There is also a more powerful filter function, select, which is a bit like MySQL database.

✘ fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq '. The article [] | select (. Author = = "tester2") '{" author ": "tester2", "title": "performanceTest" }Copy the code

The Boolean expression followed by the select function can take many other forms that are not demonstrated here.

Transformation format

In this section, I’ll share some tips for using JQ to transform original JSON data combinations into other formats.

Let’s begin by concatenating a simple JSON format:

Fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq '{(the article [0]. Title) : "FunTester"}' {" ApiTest ":" FunTester}"Copy the code

In this example, I first create a JSON object with only a pair of key-value pairs, where the key is taken from the original data in the form of (.article[0].title), which was covered in the previous article and won’t be covered here. Value I’ll just assign it to FunTester.

Note that in jQ expressions, to construct new JSON-formatted data, you need to enclose () if the key is an expression, but not if the value is an expression. This is limited to single expressions. The following example demonstrates when value is an expression.

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

The map function is used to form a new array by extracting a set of values from a node.

✘ fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq '{(the article [0]. Title) : (. The article | map. (the title)}' {" ApiTest ": [ "ApiTest", "performanceTest" ] }Copy the code

Let’s explore the use of the map function with another experiment:

Fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq '{(the article [0]. Title) : (.article | map("FunTester1","FunTester2"))}' { "ApiTest": [ "FunTester1", "FunTester2", "FunTester1", "FunTester2" ] }Copy the code

Here I write the parameters of the map function as two constants. The final output is a two-time constant, so the map function is executed twice, with the same length as the array above, and without the de-duplication function.

Next, share another function join, which is very similar to stringutils.join (), which concatenates strings in Java. The following is the use of Java Demo.

    public static String join(String text, String separator) {
        return StringUtils.join(ArrayUtils.toObject(text.toCharArray()), separator);
    }
Copy the code

Here is the use of the join function in JQ:

Fv @ FunTester  ~ / Downloads  cat FunTester. Json | jq '{(the article [0]. Title) : (.article | map("FunTester1","FunTester2") | join("-"))}' { "ApiTest": "FunTester1-FunTester2-FunTester1-FunTester2" }Copy the code

I concatenate the array I just built using the map function with a concatenate.


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

  • Automated Testing trends in 2021
  • FunTester test framework architecture diagram
  • FunTester test project architecture diagram
  • Techniques for filtering automated test cases
  • Details in the test case
  • Probe into branch problems in link pressure measurement
  • Simplify test cases
  • JMeter parameter signature – Groovy script form
  • The thread synchronization CyclicBarrier class is applied at the performance test collection point
  • Performance test error analysis text version – on
  • Performance test error analysis text version – next