# Extract value from json

**URL:** <https://community.f5.com/t/extract-value-from-json/45656>\
**Category:** Technical Forums\
**Tags:** irules, ltm, application-delivery\
**Created:** [April 26, 2020, 3:05pm UTC](https://community.f5.com/t/extract-value-from-json/45656 "2020-04-26T15:05:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoamRotter](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@NoamRotter](https://community.f5.com/u/NoamRotter)\
**Post date:** [April 26, 2020, 3:05pm UTC](https://community.f5.com/t/extract-value-from-json/45656/1 "2020-04-26T15:05:18Z")

</div>

Hi,

I have a webapp which accepts a POST with JSON payload.

the JSON has only one key:value

I need to write to log the value of the key.

For example:

This is the payload of the JSON:

```tcl
{ 
    "token": "c71c56b0-7590-4785-b490-0f060c563aee" 
} 

```

I need to write to the LTM log:

```tcl
c71c56b0-7590-4785-b490-0f060c563aee

```

I need a simple iRule which parses the payload, find the value of “token” and writes it to the ltm log.

Can someone help please?

Noam.

---

<div class="post-metadata">

**Author:** ![PeteWhite](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/petewhite/32/33768_2.png) [@PeteWhite](https://community.f5.com/u/PeteWhite)\
**Post date:** [April 28, 2020, 4:24pm UTC](https://community.f5.com/t/extract-value-from-json/45656/2 "2020-04-28T16:24:45Z")

</div>

```tcl
when HTTP_REQUEST {
  if { [HTTP::method] == "POST"} {
    HTTP::collect
  }
}
when HTTP_REQUEST_DATA {
  set token [regexp -inline -- {"token": "(.+)"} [HTTP::payload] ]
  log local0.debug "Token: $token" 
}

```

Try the above - I haven’t tested it out but it looks ok

---

<div class="post-metadata">

**Author:** ![NoamRotter](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@NoamRotter](https://community.f5.com/u/NoamRotter)\
**Post date:** [April 29, 2020, 5:36am UTC](https://community.f5.com/t/extract-value-from-json/45656/3 "2020-04-29T05:36:59Z")

</div>

Hi Pete,

Thank you for your answer.

I tested your iRule.

When using postman to post the JSON,

the postman keeps saying “Sending Request” until I hit the cancel button

I have added 2 logs line:

```tcl
when HTTP_REQUEST {
  if { [HTTP::method] == "POST"} {
  log local0. ">>> Method is POST<<<"
	HTTP::collect
    log local0. ">>> HTTP Collected<<<"
  }
}
when HTTP_REQUEST_DATA {
  set token [regexp -inline -- {"token": "(.+)"} [HTTP::payload] ]
  log local0.debug "Token: $token" 
}

```

In the ltm log I see:

Apr 29 08:27:43 bigip1 info tmm2[18399]: Rule /Common/test-iRule \<HTTP\_REQUEST\>: \>\>\> Method is POST\<\<\<

Apr 29 08:27:43 bigip1 info tmm2[18399]: Rule /Common/test-iRule \<HTTP\_REQUEST\>: \>\>\> HTTP Collected\<\<\<

When I disable the HTTP::collect line

Postman sends the request and finishes. Does not keep saying “Sending request”

But, the when HTTP\_REQUEST\_DATA part is not executed.

I moved these lines to HTTP\_REQUEST part:

set token [regexp -inline -- {“token”: “(.+)”} [HTTP::payload] ]

log local0.debug “Token: $token”

like this:

```tcl
when HTTP_REQUEST {
  if { [HTTP::method] == "POST"} {
    log local0. ">>> Method is POST<<<"
    set token [regexp -inline -- {"token": "(.+)"} [HTTP::payload] ]
    log local0.debug "Token: $token" 
  }
}

```

And now I get in the ltm log:

Apr 29 08:33:45 bigip1 debug tmm2[18399]: Rule /Common/test-iRule \<HTTP\_REQUEST\>: Token: {“token”: “01593317-66f9-4e8a-8467-6f4652e680e3”} 01593317-66f9-4e8a-8467-6f4652e680e3

And I expected to get:

Apr 29 08:33:45 bigip1 debug tmm2[18399]: Rule /Common/test-iRule \<HTTP\_REQUEST\>: Token: 01593317-66f9-4e8a-8467-6f4652e680e3

Thank you for your time.

Noam.

---

<div class="post-metadata">

**Author:** ![PeteWhite](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/petewhite/32/33768_2.png) [@PeteWhite](https://community.f5.com/u/PeteWhite)\
**Post date:** [April 29, 2020, 8:58am UTC](https://community.f5.com/t/extract-value-from-json/45656/4 "2020-04-29T08:58:47Z")

</div>

Great! HTTP::payload must be able to retrieve it because it comes in a single packet so it doesn’t need to accumulate. Note that if the payload was larger ( eg 1K ) then this might fail and you’d have to use HTTP::collect.

We can use regexp in multiple ways to retrieve the substring. Try this:

```tcl
when HTTP_REQUEST {
  if { [HTTP::method] == "POST"} {
    log local0. ">>> Method is POST<<<"
    if { [regexp -- {"token": "(.+)"} [HTTP::payload] a b ] } {
        log local0.debug "Token Found a:$a b:$b"
    }
  }
}

```

$a should be the whole of the line, $b should be only the token part.

Take a look [here](https://www.tutorialspoint.com/tcl-tk/tcl_regular_expressions.htm) for more detail on how to use regexp:

PS the other way we could have done it is this:

```tcl
set token [lindex [regexp -inline -- {"token": "(.+)"} [HTTP::payload] ] 1]

```

---

<div class="post-metadata">

**Author:** ![NoamRotter](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@NoamRotter](https://community.f5.com/u/NoamRotter)\
**Post date:** [April 29, 2020, 9:30am UTC](https://community.f5.com/t/extract-value-from-json/45656/5 "2020-04-29T09:30:54Z")

</div>

Hi Pete,

Thank you very much,

This is working as well as the PS.

👍

---

<div class="post-metadata">

**Author:** ![PeteWhite](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/petewhite/32/33768_2.png) [@PeteWhite](https://community.f5.com/u/PeteWhite)\
**Post date:** [April 29, 2020, 10:31am UTC](https://community.f5.com/t/extract-value-from-json/45656/6 "2020-04-29T10:31:14Z")

</div>

Excellent! Take the rest of the day off 🙂
