github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/md/gjson.en.md (about) 1 ### `gjson()` {#fn-gjson} 2 3 Function prototype: `fn gjson(input, json_path: str, newkey: str)` 4 5 Function description: Extract specified fields from JSON, rename them as new fields, and ensure they are arranged in the original order. 6 7 Function parameters: 8 9 - `input`: The JSON to be extracted can either be the original text (`_`) or a specific `key` after the initial extraction. 10 - `json_path`: JSON path information 11 - `newkey`: Write the data to the new key after extraction 12 13 ```python 14 # Directly extract the field x.y from the original input JSON and rename it as a new field abc. 15 gjson(_, "x.y", "abc") 16 17 # Extract the x.y field from a previously extracted key, and name the extracted field as x.y. 18 gjson(key, "x.y") 19 20 # Extract arrays, where `key` and `abc` are arrays. 21 gjson(key, "1.abc.2") 22 ``` 23 24 Example 1: 25 26 ```python 27 # input data: 28 # {"info": {"age": 17, "name": "zhangsan", "height": 180}} 29 30 # script: 31 gjson(_, "info", "zhangsan") 32 gjson(zhangsan, "name") 33 gjson(zhangsan, "age", "age") 34 35 # result: 36 { 37 "age": 17, 38 "message": "{\"info\": {\"age\": 17, \"name\": \"zhangsan\", \"height\": 180}}", 39 "name": "zhangsan", 40 "zhangsan": "{\"age\":17,\"height\":180,\"name\":\"zhangsan\"}" 41 } 42 ``` 43 44 Example 2: 45 46 ```python 47 # input data: 48 # data = { 49 # "name": {"first": "Tom", "last": "Anderson"}, 50 # "age":37, 51 # "children": ["Sara","Alex","Jack"], 52 # "fav.movie": "Deer Hunter", 53 # "friends": [ 54 # {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, 55 # {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, 56 # {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} 57 # ] 58 # } 59 60 # script: 61 gjson(_, "name") 62 gjson(name, "first") 63 ``` 64 65 Example 3: 66 67 ```python 68 # input data: 69 # [ 70 # {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, 71 # {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, 72 # {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} 73 # ] 74 75 # scripts for JSON list: 76 gjson(_, "0.nets.1") 77 ```