测试人社区

python接口测试jsonpath,从接口的返回参数中获取特定的值传入下一个接口的入参

背景:目前在工作中要实现接口测试的参数化,需要从接口A的返回参数中获取相应的值,存在列表中,给接口B的传参参数化调用
遇到的问题:接口A的返回参数目前想拿到特定的key值遇到问题
接口A的返回参数如下:

[
  {
    "nodes": [
      {
        "nodes": [
        ],
        "leaves": [
          {
            "name": "脚本版本",
            "key": "TrackerVersion",
            "dataType": 1,
            "columnTypes": 0,
            "formatDataType": 0,
            "formatString": null,
            "tag": {
              "ColumnGroupType": "Dimension",
              "IsDeleted": false,
              "HideInUI": true,
              "DimMetricsType": "BaseDimension",
              "MetadataExpression": "脚本版本",
              "SqlBuilderInfo": {
                "Key": null,
                "Tables": [
                  "SessionView",
                  "InternalLinkClick",
                  "Exposure",
                  "PageViewView",
                  "ECommerceView",
                  "MouseClickView",
                  "ConversionView",
                  "ConversionEventView",
                  "EcomProductView",
                  "SiteSearch",
                  "SiteSearchClick",
                  "ABTest",
                  "ConversionPathStepView",
                  "UserBehaviorView"
                ],
                "FirstTable": "SessionView",
                "Columns": [
                  "tracker_version"
                ],
                "DataType": 0,
                "IsNvarchar": false,
                "NotSupportDrilldown": false,
                "FirstColumn": "tracker_version",
                "ExtendedColumn": null,
                "ExtendedSql": null,
                "IsExtendedDimension": false,
                "PreAggregateColumn": null,
                "PostAggregateColumn": null,
                "Sql": null,
                "EquivalentMeasures": null,
                "IsDimension": false,
                "IsJoinDimension": false,
                "SelectSql": " AS ",
                "IsManyTableExists": true,
                "CalculateBaseMetrics": null,
                "CalculateMetricsExpression": null,
                "IsCalculateMetric": false,
                "IsManyToManyDim": false,
                "IsDistinctMeasure": false
              },
              "HasPermission": true
            }
          },
          {
            "name": "配置文件类别",
            "key": "ProfileGroup",
            "dataType": 1,
            "columnTypes": 0,
            "formatDataType": 0,
            "formatString": null,
            "tag": {
              "ColumnGroupType": "Mesures",
              "IsDeleted": false,
              "HideInUI": true,
              "DimMetricsType": "BaseDimension",
              "MetadataExpression": "配置文件类别",
              "SqlBuilderInfo": {
                "Key": null,
                "Tables": [
                  "SessionView",
                  "InternalLinkClick",
                  "ConversionParamsView",
                  "ConversionEventView",
                  "Exposure",
                  "PageViewView",
                  "ScreenFocus",
                  "MouseScrollView",
                  "ECommerceView",
                  "MouseClickView",
                  "UserBehaviorView",
                  "ConversionView",
                  "EcomProductView",
                  "SiteSearch",
                  "SiteSearchClick",
                  "ABTest",
                  "ConversionPathStepView",
                  "Navigation_PV"
                ],
                "FirstTable": "SessionView",
                "Columns": [
                  "profile_group"
                ],
                "DataType": 0,
                "IsNvarchar": false,
                "NotSupportDrilldown": false,
                "FirstColumn": "profile_group",
                "ExtendedColumn": null,
                "ExtendedSql": null,
                "IsExtendedDimension": false,
                "PreAggregateColumn": null,
                "PostAggregateColumn": null,
                "Sql": null,
                "EquivalentMeasures": null,
                "IsDimension": false,
                "IsJoinDimension": false,
                "SelectSql": " AS ",
                "IsManyTableExists": true,
                "CalculateBaseMetrics": null,
                "CalculateMetricsExpression": null,
                "IsCalculateMetric": false,
                "IsManyToManyDim": false,
                "IsDistinctMeasure": false
              },
              "HasPermission": true
            }
          }
        ]
      }
    ]
  }
]

问题:根据ColumnGroupType==Dimension或者Mesures的不同,分别拿到所有Dimension对应的key值全部放在一个列表中,拿到所有Mesures对应的key值全部放到另外一个列表中

jsonpath在线调试

建议去jsonpath在线调试网站实时试验,体验比用代码验证好

jsonpath

$..leaves[?(@.tag.ColumnGroupType=='Dimension')].name