github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/md/json.en.md (about) 1 ### `json()` {#fn-json} 2 3 Function prototype: `fn json(input: str, json_path, newkey, trim_space: bool = true)` 4 5 Function description: Extract the specified field in JSON and name it as a new field. 6 7 Function parameters: 8 9 - `input`: The JSON to be extracted can be the original text (`_`) or a `key` after the initial extraction 10 - `json_path`: JSON path information 11 - `newkey`:Write the data to the new key after extraction 12 - `trim_space`: Delete the leading and trailing blank characters in the extracted characters, the default value is true 13 - `delete_after_extract`: After extract delete the extracted info from input. Only map key and map value are deletable, list(array) are not supported. Default is `false'. 14 15 ```python 16 # Directly extract the x.y field in the original input json, and name it as a new field abc 17 json(_, x.y, abc) 18 19 # For a `key` that has been extracted, extract `x.y` again, and the extracted field name is `x.y` 20 json(key, x.y) 21 ``` 22 23 Example 1: 24 25 ```python 26 # input data: 27 # {"info": {"age": 17, "name": "zhangsan", "height": 180}} 28 29 # script: 30 json(_, info, "zhangsan") 31 json(zhangsan, name) 32 json(zhangsan, age, "age") 33 34 # result: 35 { 36 "age": 17, 37 "message": "{\"info\": {\"age\": 17, \"name\": \"zhangsan\", \"height\": 180}}", 38 "name": "zhangsan", 39 "zhangsan": "{\"age\":17,\"height\":180,\"name\":\"zhangsan\"}" 40 } 41 ``` 42 43 Example 2: 44 45 ```python 46 # input data: 47 # data = { 48 # "name": {"first": "Tom", "last": "Anderson"}, 49 # "age":37, 50 # "children": ["Sara","Alex","Jack"], 51 # "fav.movie": "Deer Hunter", 52 # "friends": [ 53 # {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, 54 # {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, 55 # {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} 56 # ] 57 # } 58 59 # script: 60 json(_, name) 61 json(name, first) 62 ``` 63 64 Example 3: 65 66 ```python 67 # input data: 68 # [ 69 # {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]}, 70 # {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]}, 71 # {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]} 72 # ] 73 74 # script: 75 json(_, .[0].nets[-1]) 76 ``` 77 78 Example 4: 79 80 ```python 81 # input data: 82 {"item": " not_space ", "item2":{"item3": [123]}} 83 84 # script: 85 json(_, item2.item3, item, delete_after_extract = true) 86 87 # result: 88 { 89 "item": "[123]", 90 "message": "{\"item\":\" not_space \",\"item2\":{}}", 91 } 92 ``` 93 94 95 Example 5: 96 97 ```python 98 # input data: 99 {"item": " not_space ", "item2":{"item3": [123]}} 100 101 # If you try to remove a list element it will fail the script check. 102 # Script: 103 json(_, item2.item3[0], item, delete_after_extract = true) 104 105 106 # test command: 107 # datakit pipeline -P j2.p -T '{"item": " not_space ", "item2":{"item3": [123]}}' 108 # report error: 109 # [E] j2.p:1:54: does not support deleting elements in the list 110 ```