简体   繁体   English

从JSON仅提取特定流程的一个字段

[英]Extract only one field for a specific flow from JSON

Below is the Json I receive as Response from url. 以下是我从url作为响应收到的Json。

{"flows":[{"version":"OF_13","cookie":"0","tableId":"0x0","packetCount":"24","byteCount":"4563","durationSeconds":"5747","priority":"0","idleTimeoutSec":"0","hardTimeoutSec":"0","flags":"0","match":{},"instructions":{"instruction_apply_actions":{"actions":"output=controller"}}},

{"version":"OF_13","cookie":"45036000240104713","tableId":"0x0","packetCount":"0","byteCount":"0","durationSeconds":"29","priority":"6","idleTimeoutSec":"0","hardTimeoutSec":"0","flags":"1","match":{"eth_type":"0x0x800","ipv4_src":"10.0.0.10","ipv4_dst":"10.0.0.12"},"instructions":{"none":"drop"}},

{"version":"OF_13","cookie":"45036000240104714","tableId":"0x0","packetCount":"0","byteCount":"0","durationSeconds":"3","priority":"7","idleTimeoutSec":"0","hardTimeoutSec":"0","flags":"1","match":{"eth_type":"0x0x800","ipv4_src":"10.0.0.10","ipv4_dst":"127.0.0.1"},"instructions":{"none":"drop"}},

{"version":"OF_13","cookie":"0","tableId":"0x1","packetCount":"0","byteCount":"0","durationSeconds":"5747","priority":"0","idleTimeoutSec":"0","hardTimeoutSec":"0","flags":"0","match":{},"instructions":{"instruction_apply_actions":{"actions":"output=controller"}}}]}

So, I have for example four flows and I want to extract only the field " byteCount " for a specific flow identify by the ipv4_src and ipv4_dst that i have to give it as input 因此,例如,我有四个流,我只想提取特定流的字段“ byteCount ”,该特定流由ipv4_srcipv4_dst标识 ,我必须将其作为输入

How can I do this? 我怎样才能做到这一点?

json_array := JSON.parse(json_string)
foreach (element in json_array.flows):
    if(element.match.hasProperty('ipv4_src') && element.match.hasProperty('ipv4_dst')):
        if(element.match.ipv4_src == myValue && element.match.ipv4_dst == otherValue):
            print element.byteCount ;

The above is a pseudo-code to find byteCount based on ipv4_src and ipv4_dst . 上面是一个伪代码, byteCount基于ipv4_srcipv4_dst查找ipv4_dst Note that these two properties are within match property, which may or may not contain them. 请注意,这两个属性在match属性中,可能包含也可能不包含。 Hence, first check for their existence and then process. 因此,首先检查它们的存在,然后进行处理。

Note: When formatted property, each element in the array is like 注意:设置格式属性后,数组中的每个元素都类似于

{  
     "version":"OF_13",
     "cookie":"45036000240104713",
     "tableId":"0x0",
     "packetCount":"0",
     "byteCount":"0",
     "durationSeconds":"29",
     "priority":"6",
     "idleTimeoutSec":"0",
     "hardTimeoutSec":"0",
     "flags":"1",
     "match":{  
        "eth_type":"0x0x800",
        "ipv4_src":"10.0.0.10",
        "ipv4_dst":"10.0.0.12"
     },
     "instructions":{  
        "none":"drop"
     }
}

Here's how to perform the selection and extraction task using the command-line tool jq : 以下是使用命令行工具jq执行选择和提取任务的方法:

First create a file, say "extract.jq", with these three lines: 首先使用以下三行创建一个文件“ extract.jq”:

.flows[]
| select(.match.ipv4_src == $src and .match.ipv4_dst == $dst)
| [$src, $dst, .byteCount]

Next, assuming the desired src and dst are 10.0.0.10 and 10.0.0.12 respectively, and that the input is in a file named input.json, run this command: 接下来,假设所需的src和dst分别为10.0.0.10和10.0.0.12,并且输入位于名为input.json的文件中,请运行以下命令:

jq -c --arg src 10.0.0.10 --arg dst 10.0.0.12 -f extract.jq input.json

This would produce one line per match; 每场比赛将产生一行; in the case of your example, it would produce: 在您的示例中,它将产生:

["10.0.0.10","10.0.0.12","0"]

If the JSON is coming from some command (such as curl), you can use a pipeline along the following lines: 如果JSON来自某些命令(例如curl),则可以使用以下几行的管道:

curl ... | jq -c --arg src 10.0.0.10 --arg dst 10.0.0.12 -f extract.jq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM