Egrep special expressions like \w in bracket expressions [] - regex

I am trying to use extended grep to extract data from a JSON. The regex I use is functional on my regexr instance, but for some reason it doesn't work in bash.
I tried many things, notably the bare double dash and various minor edits to the regex for escaping.
#!/bin/bash
networks='{ "networks": [ { "admin_state_up": true, "availability_zone_hints": [], "availability_zones": [], "created_at": "2019-03-12T23:45:13Z", "description": "", "id": "7188504a-72cb-4590-a9b0-414732017837", "ipv4_address_scope": null, "ipv6_address_scope": null, "is_default": false, "mtu": 1450, "name": "BLUE", "port_security_enabled": true, "project_id": "187d635aec4c43fe8e8918afb3a5c82e", "provider:network_type": "vxlan", "provider:physical_network": null, "provider:segmentation_id": 86, "revision_number": 2, "router:external": false, "shared": false, "status": "ACTIVE", "subnets": [], "tags": [], "tenant_id": "187d635aec4c43fe8e8918afb3a5c82e", "updated_at": "2019-03-12T23:45:13Z" }, { "admin_state_up": true, "availability_zone_hints": [], "availability_zones": [], "created_at": "2019-03-12T23:45:13Z", "description": "", "id": "ed82083f-0a7c-4322-a4fb-de8db23e2bae", "ipv4_address_scope": null, "ipv6_address_scope": null, "is_default": false, "mtu": 1450, "name": "RED", "port_security_enabled": true, "project_id": "187d635aec4c43fe8e8918afb3a5c82e", "provider:network_type": "vxlan", "provider:physical_network": null, "provider:segmentation_id": 108, "revision_number": 2, "router:external": false, "shared": false, "status": "ACTIVE", "subnets": [], "tags": [], "tenant_id": "187d635aec4c43fe8e8918afb3a5c82e", "updated_at": "2019-03-12T23:45:13Z" }, { "admin_state_up": true, "availability_zone_hints": [], "availability_zones": [], "created_at": "2019-03-12T23:45:13Z", "description": "", "id": "1eb6647e-869e-4e83-9468-43e2c320bccc", "ipv4_address_scope": null, "ipv6_address_scope": null, "is_default": false, "mtu": 1450, "name": "public", "port_security_enabled": true, "project_id": "187d635aec4c43fe8e8918afb3a5c82e", "provider:network_type": "vxlan", "provider:physical_network": null, "provider:segmentation_id": 32, "revision_number": 2, "router:external": false, "shared": false, "status": "ACTIVE", "subnets": [], "tags": [], "tenant_id": "187d635aec4c43fe8e8918afb3a5c82e", "updated_at": "2019-03-12T23:45:13Z" } ] }'
result=`echo $networks | grep -oE '"(id|name)": "([\w+-]+)"'`
echo $result
The aforementioned code doesn't work but if I switch to the following regex, it works. I just need to add extraction for id field too to be able to extract ids and names using \2 back reference (group 2)
grep -oE '"(id|name)": "(\w+)"'
Can you help me understand why the script doesn't work?
Full formatted JSON
{
"networks": [{
"admin_state_up": true,
"availability_zone_hints": [],
"availability_zones": [],
"created_at": "2019-03-12T23:45:13Z",
"description": "",
"id": "7188504a-72cb-4590-a9b0-414732017837",
"ipv4_address_scope": null,
"ipv6_address_scope": null,
"is_default": false,
"mtu": 1450,
"name": "BLUE",
"port_security_enabled": true,
"project_id": "187d635aec4c43fe8e8918afb3a5c82e",
"provider:network_type": "vxlan",
"provider:physical_network": null,
"provider:segmentation_id": 86,
"revision_number": 2,
"router:external": false,
"shared": false,
"status": "ACTIVE",
"subnets": [],
"tags": [],
"tenant_id": "187d635aec4c43fe8e8918afb3a5c82e",
"updated_at": "2019-03-12T23:45:13Z"
}, {
"admin_state_up": true,
"availability_zone_hints": [],
"availability_zones": [],
"created_at": "2019-03-12T23:45:13Z",
"description": "",
"id": "ed82083f-0a7c-4322-a4fb-de8db23e2bae",
"ipv4_address_scope": null,
"ipv6_address_scope": null,
"is_default": false,
"mtu": 1450,
"name": "RED",
"port_security_enabled": true,
"project_id": "187d635aec4c43fe8e8918afb3a5c82e",
"provider:network_type": "vxlan",
"provider:physical_network": null,
"provider:segmentation_id": 108,
"revision_number": 2,
"router:external": false,
"shared": false,
"status": "ACTIVE",
"subnets": [],
"tags": [],
"tenant_id": "187d635aec4c43fe8e8918afb3a5c82e",
"updated_at": "2019-03-12T23:45:13Z"
}, {
"admin_state_up": true,
"availability_zone_hints": [],
"availability_zones": [],
"created_at": "2019-03-12T23:45:13Z",
"description": "",
"id": "1eb6647e-869e-4e83-9468-43e2c320bccc",
"ipv4_address_scope": null,
"ipv6_address_scope": null,
"is_default": false,
"mtu": 1450,
"name": "public",
"port_security_enabled": true,
"project_id": "187d635aec4c43fe8e8918afb3a5c82e",
"provider:network_type": "vxlan",
"provider:physical_network": null,
"provider:segmentation_id": 32,
"revision_number": 2,
"router:external": false,
"shared": false,
"status": "ACTIVE",
"subnets": [],
"tags": [],
"tenant_id": "187d635aec4c43fe8e8918afb3a5c82e",
"updated_at": "2019-03-12T23:45:13Z"
}]
}

According to man grep:
The Backslash Character and Special Expressions
The symbol \w is a synonym for [[:alnum:]] and \W is a synonym for [^[:alnum:]]. ... A bracket expression is a list of characters enclosed by [ and ]. ... To include a literal ] place it first in the list. Similarly, to include a literal ^ place it anywhere but first. Finally, to include a literal - place it last.
Basically, \w is literally replaced by those characters when evaluated, giving you "([[[:alnum:]]+-]+)", which in a US standard locale gives you "([[a-zA-Z0-9]+-]+)".
Since a bracket expression is truncated by the first ] it sees (unless it is the first element of a bracket expression), the group is only [[[:alnum:]]+, or "1 or more of a digit, letter, and [. This expression is followed by -]+, meaning "exactly one hyphen and one or more ]". This is obviously pretty terrible.
If you try
echo $networks | grep -oE '"(id|name)": "([[:alnum:]+-]+)"'
I.e., \w without the outer bracket expression, the relevant part means "a group (surrounded by ") comprised of one or more digits, letters, hyphens, and plus signs", which outputs:
"id": "7188504a-72cb-4590-a9b0-414732017837"
"name": "BLUE"
"id": "ed82083f-0a7c-4322-a4fb-de8db23e2bae"
"name": "RED"
"id": "1eb6647e-869e-4e83-9468-43e2c320bccc"
"name": "public"

Using PERL (-P) instead of Extended (-E) regexp, looks like the \w is interpreted as expected, without escaping issue: note the -oP
result=$( echo $networks | grep -oP '"(id|name)": "([\w+-]+)"' ) ;
echo $result
"id": "7188504a-72cb-4590-a9b0-414732017837" "name": "BLUE" "id": "ed82083f-0a7c-4322-a4fb-de8db23e2bae" "name": "RED" "id": "1eb6647e-869e-4e83-9468-43e2c320bccc" "name": "public"

As a workaround (it does not resolve the "escaping \w issue)
result=$( echo $networks | grep -oE '"(id|name)": "([a-zA-Z_+-]+)"' ) ;
echo $result
Prints me:
"name": "BLUE" "name": "RED" "name": "public"
Note: prefer using $( ) syntax to execute sub shells rather than the backtick.

Related

Parsing complex JSON using Kinesis Analytics

I have the following JSON stream coming from Twitter.
{
"created_at": "Thu Sep 27 21:02:00 +0000 2018",
"id": 1045418301336244224,
"id_str": "1045418301336244224",
"text": "Conditional Branching Now Supported in AWS Systems Manager Automation - #awscloud #amazon #aws",
"source": "Buffer",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 14687423,
"id_str": "14687423",
"name": "Casey Becking",
"screen_name": "caseybecking",
"location": "Huntington Beach, CA",
"url": "http://caseybecking.com",
"description": "I do stuff with computers for #rackspace , geek at heart! play and watch to much hockey, someday I'll make a personal website.",
"translator_type": "none",
"protected": false,
"verified": false,
"followers_count": 4191,
"friends_count": 2412,
"listed_count": 90,
"favourites_count": 794,
"statuses_count": 12995,
"created_at": "Wed May 07 15:03:23 +0000 2008",
"utc_offset": null,
"time_zone": null,
"geo_enabled": true,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"profile_background_color": "000000",
"profile_background_image_url": "http://abs.twimg.com/images/themes/theme15/bg.png",
"profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme15/bg.png",
"profile_background_tile": false,
"profile_link_color": "ABB8C2",
"profile_sidebar_border_color": "000000",
"profile_sidebar_fill_color": "000000",
"profile_text_color": "000000",
"profile_use_background_image": false,
"profile_image_url": "http://pbs.twimg.com/profile_images/981617292546060289/RMX0GQFe_normal.jpg",
"profile_image_url_https": "https://pbs.twimg.com/profile_images/981617292546060289/RMX0GQFe_normal.jpg",
"profile_banner_url": "https://pbs.twimg.com/profile_banners/14687423/1439137746",
"default_profile": false,
"default_profile_image": false,
"following": null,
"follow_request_sent": null,
"notifications": null
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"quote_count": 0,
"reply_count": 0,
"retweet_count": 0,
"favorite_count": 0,
"entities": {
"hashtags": [{
"text": "amazon",
"indices": [106, 113]
}, {
"text": "aws",
"indices": [114, 118]
}],
"urls": [{
"url": "",
"expanded_url": "https://buff.ly/2zwRyBx",
"display_url": "buff.ly/2zwRyBx",
"indices": [72, 95]
}],
"user_mentions": [{
"screen_name": "awscloud",
"name": "Amazon Web Services",
"id": 66780587,
"id_str": "66780587",
"indices": [96, 105]
}],
"symbols": []
},
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"filter_level": "low",
"lang": "en",
"timestamp_ms": "1538082120628",
"emoticons": [],
"sentiments": "Neutral"
}
How do I parse, analyze and process this JSON using Kinesis Analytics?
The arrays should be flattened and this is very doable in Hive but need to do the same in Kinesis Analytics.

Put in double quotes all between two patterns in every line

I have ndjson file, with strings like this:
{"created": "2016-03-08 00:00:00 UTC", "changed": "2016-03-08 08:51:56 UTC", "rev": 28990, "status": 1, "user": [{"user_id": null, "name": null, "loyaltyCard": 123456789012}], "id": "26680533564", "tax": null, "products": [{"price": 289, "quantity": 1, "coupon": null, "id": "4122"}], "shipping": 0.0}
I need to take in double quotes values of "loyaltyCard", considering, that it could be digits, letters (cyrillic also) and anything else.
Expecting to see something like:
UTC", "rev": 280, "status": 1, "user": [{"user_id": null, "name": null, "loyaltyCard": "123456789012"}], "id": "26680533564", "tax": null, "products": [{"price": 289, "quantity": 1, "coupon": null, "id": "4122"}], "shipping": 0.0}
UTC", "rev": 56990, "status": 1, "user": [{"user_id": 543445, "name": null, "loyaltyCard": "1233bla456bla"}], "id": "5454580534", "tax": null, "products": [{"price": 869, "quantity": 2, "coupon": null, "id": "86854"}], "shipping": 0.0}
If you want to wrap up loyaltyCard value in double quotes, you can use a regex like this:
(loyaltyCard": )([^}]*)\}
With a replacement string:
\1"\2"}
Regex demo
Update for Vim: to find/replace in vim you can use:
:%s/\(loyaltyCard": \)\([^}]*\)\}/\1"\2"}/
^ ^----------------------------^ ^-----^
| + Pattern1 (w/capture groups) + Pattern 2 (w/ group refs)
+ substitute cmd
s/: (\d+)/: "$1"/ might do the trick
This is one of the way:
m/"loyaltyCard"\:\s*"([^"]*)"/g;
If you want to give a escape sequence for quotes your wish.

Strongloop PostgreSQL connector embedded model error

Got a simple Loopback API to retrieve tickets and responses using the postgreSQL connector. Tickets and responses come back fine individually but when I try to embed the responses into the ticket model I get the error below. I have tried following the documentation and I'm sure it's something simple in one of my relations that I'm missing but whatever I try, I cannot get it to work.
Any help would be appreciated.
https://docs.strongloop.com/display/public/LB/Embedded+models+and+relations#Embeddedmodelsandrelations-EmbedsMany
Ticket Model:
{
"name": "Ticket",
"base": "PersistedModel",
"idInjection": true,
"options": {
"postgresql": {
"schema": "customer_service",
"table": "tbl_ticket"
}
},
"properties": {
"description": {
"type": "String",
"required": true,
"length": null,
"precision": null,
"scale": null,
"postgresql": {
"columnName": "description",
"dataType": "text",
"dataLength": null,
"dataPrecision": null,
"dataScale": null,
"nullable": "NO"
}
},
"id": {
"type": "Number",
"id": 1,
"required": true,
"length": null,
"precision": 32,
"scale": 0,
"postgresql": {
"columnName": "id",
"dataType": "integer",
"dataLength": null,
"dataPrecision": 32,
"dataScale": 0,
"nullable": "NO"
}
}
},
"validations": [],
"relations": {
"responses": {
"type": "embedsMany",
"model": "Response",
"property": "embededResponses",
"options": {
"validate": true,
"forceId": false
}
}
},
"acls": [],
"methods": {}
}
Response Model:
{
"name": "Response",
"base": "PersistedModel",
"idInjection": true,
"options": {
"postgresql": {
"schema": "customer_service",
"table": "tbl_response"
}
},
"properties": {
"notes": {
"type": "String",
"required": false,
"length": null,
"precision": null,
"scale": null,
"postgresql": {
"columnName": "notes",
"dataType": "text",
"dataLength": null,
"dataPrecision": null,
"dataScale": null,
"nullable": "YES"
}
},
"ticketId": {
"type": "Number",
"required": true,
"length": null,
"precision": 32,
"scale": 0,
"postgresql": {
"columnName": "ticket_id",
"dataType": "integer",
"dataLength": null,
"dataPrecision": 32,
"dataScale": 0,
"nullable": "NO"
}
},
"id": {
"type": "Number",
"id": 1,
"required": true,
"length": null,
"precision": 32,
"scale": 0,
"postgresql": {
"columnName": "id",
"dataType": "integer",
"dataLength": null,
"dataPrecision": 32,
"dataScale": 0,
"nullable": "NO"
}
}
},
"validations": [],
"relations": {
"ticket": {
"type": "belongsTo",
"model": "Ticket",
"foreignKey": "ticketId"
}
},
"acls": [],
"methods": {}
}
Error:
{
"error": {
"name": "error",
"status": 500,
"message": "column \"embededresponses\" does not exist",
"length": 126,
"severity": "ERROR",
"code": "42703",
"position": "213",
"file": ".\\src\\backend\\parser\\parse_expr.c",
"line": "766",
"routine": "transformColumnRef",
"stack": "error: column \"embededresponses\" does not exist\n at Connection.parseE (C:\\WebApp\\node_modules\\loopback-connector-postgresql\\node_modules\\pg\\lib\\connection.js:539:11)\n at Connection.parseMessage (C:\\WebApp\\node_modules\\loopback-connector-postgresql\\node_modules\\pg\\lib\\connection.js:366:17)\n at Socket.<anonymous> (C:\\WebApp\\node_modules\\loopback-connector-postgresql\\node_modules\\pg\\lib\\connection.js:105:22)\n at Socket.emit (events.js:107:17)\n at readableAddChunk (_stream_readable.js:163:16)\n at Socket.Readable.push (_stream_readable.js:126:10)\n at TCP.onread (net.js:538:20)"
}
}
Your Ticket model should have the following relationship section:
"relations": {
"Responses": {
"type": "hasMany",
"model": "Response",
"foreignKey": "ticketId"
}
}
Your Response model relationship is correct.
It's not very clear from the docs that the embed relationships are for NoSQL databases. For traditional SQL databases, use the Has* relationship types.
To retrieve a Ticket with Responses from the REST API use the include filter: https://docs.strongloop.com/display/public/LB/Include+filter.
Example: localhost:3000/api/Tickets/{id}?filter[include]=responses
I am told that you can use embedded relations with SQL data sources, but the data is then stored in stringified-JSON format.
I've added a note to https://docs.strongloop.com/display/LB/Embedded+models+and+relations.
Rand

Notepad++ close non-whitespace

I have a Tweepy Stream Api json file that is included 33K tweets. All of them have non-whitespace space that I couldn't find solution. If you use copy and past it to pro.jsonlint.com, you can see that line 217 has "EOF" problem. I can fix them using notepad++ but first I need to understand that what is the problem and how I can fix it?
If somebody can tell me where is the EOF problem and how fix it with notepad++.
Thanks,
{
"created_at": "Tue Mar 31 20:50:08 +0000 2015",
"id": 583008398612029440,
"id_str": "583008398612029440",
"text": "RT #kamalakmustafa: Hain bir sald\u0131r\u0131 sonucu \u015fehit d\u00fc\u015fen Savc\u0131m\u0131z #MehmetSelimKiraz 'a Allah'tan rahmet, ailesine sab\u0131rlar diliyorum. Millet\u2026",
"source": "\u003ca href=\"http:\/\/www.twitter.com\" rel=\"nofollow\"\u003eTwitter for Windows Phone\u003c\/a\u003e",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 1337409816,
"id_str": "1337409816",
"name": "Atakan \u00c7etin",
"screen_name": "BrownnChild",
"location": "",
"url": null,
"description": "Bir nefesine bile h\u00fckmedemedi\u011fimiz bu d\u00fcnya i\u00e7in boyun mu b\u00fckece\u011fiz? M\u0130LL\u0130 G\u00d6R\u00dc\u015e!",
"protected": false,
"verified": false,
"followers_count": 246,
"friends_count": 116,
"listed_count": 0,
"favourites_count": 1077,
"statuses_count": 2613,
"created_at": "Mon Apr 08 19:31:06 +0000 2013",
"utc_offset": 10800,
"time_zone": "Athens",
"geo_enabled": true,
"lang": "tr",
"contributors_enabled": false,
"is_translator": false,
"profile_background_color": "131516",
"profile_background_image_url": "http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_image_url_https": "https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif",
"profile_background_tile": true,
"profile_link_color": "009999",
"profile_sidebar_border_color": "EEEEEE",
"profile_sidebar_fill_color": "EFEFEF",
"profile_text_color": "333333",
"profile_use_background_image": true,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/579347764670296064\/6yuzFJPG_normal.jpg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/579347764670296064\/6yuzFJPG_normal.jpg",
"profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/1337409816\/1427653680",
"default_profile": false,
"default_profile_image": false,
"following": null,
"follow_request_sent": null,
"notifications": null
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweeted_status": {
"created_at": "Tue Mar 31 20:38:15 +0000 2015",
"id": 583005408828252160,
"id_str": "583005408828252160",
"text": "Hain bir sald\u0131r\u0131 sonucu \u015fehit d\u00fc\u015fen Savc\u0131m\u0131z #MehmetSelimKiraz 'a Allah'tan rahmet, ailesine sab\u0131rlar diliyorum. Milletimizin ba\u015f\u0131 sa\u011folsun.",
"source": "\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 338288811,
"id_str": "338288811",
"name": "Mustafa Kamalak",
"screen_name": "kamalakmustafa",
"location": "Ankara, T\u00fcrkiye",
"url": "http:\/\/www.saadet.org.tr\/kisi\/mustafa-kamalak",
"description": "Saadet Partisi Genel Ba\u015fkan\u0131\n Prof. Dr. Mustafa Kamalak",
"protected": false,
"verified": false,
"followers_count": 52358,
"friends_count": 2,
"listed_count": 163,
"favourites_count": 0,
"statuses_count": 1574,
"created_at": "Tue Jul 19 10:48:16 +0000 2011",
"utc_offset": 10800,
"time_zone": "Istanbul",
"geo_enabled": false,
"lang": "tr",
"contributors_enabled": false,
"is_translator": false,
"profile_background_color": "FFFFFF",
"profile_background_image_url": "http:\/\/pbs.twimg.com\/profile_background_images\/378800000181217982\/FBktDfqo.jpeg",
"profile_background_image_url_https": "https:\/\/pbs.twimg.com\/profile_background_images\/378800000181217982\/FBktDfqo.jpeg",
"profile_background_tile": false,
"profile_link_color": "0084B4",
"profile_sidebar_border_color": "000000",
"profile_sidebar_fill_color": "DDFFCC",
"profile_text_color": "333333",
"profile_use_background_image": true,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/436119860225908737\/76GmiIPj_normal.jpeg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/436119860225908737\/76GmiIPj_normal.jpeg",
"profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/338288811\/1423922000",
"default_profile": false,
"default_profile_image": false,
"following": null,
"follow_request_sent": null,
"notifications": null
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweet_count": 122,
"favorite_count": 152,
"entities": {
"hashtags": [
{
"text": "MehmetSelimKiraz",
"indices": [
45,
62
]
}
],
"trends": [
],
"urls": [
],
"user_mentions": [
],
"symbols": [
]
},
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"filter_level": "low",
"lang": "tr"
},
"retweet_count": 0,
"favorite_count": 0,
"entities": {
"hashtags": [
{
"text": "MehmetSelimKiraz",
"indices": [
65,
82
]
}
],
"trends": [
],
"urls": [
],
"user_mentions": [
{
"screen_name": "kamalakmustafa",
"name": "Mustafa Kamalak",
"id": 338288811,
"id_str": "338288811",
"indices": [
3,
18
]
}
],
"symbols": [
]
},
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"filter_level": "low",
"lang": "tr",
"timestamp_ms": "1427835008658"
}
{
"created_at": "Tue Mar 31 20:50:08 +0000 2015",
"id": 583008398649769984,
"id_str": "583008398649769984",
"text": "RT #BekirDeveli: #MehmetSelimKiraz",
"source": "\u003ca href=\"http:\/\/twitterhizmetcisi.com\" rel=\"nofollow\"\u003eTranslation Mobile\u003c\/a\u003e",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 1284184062,
"id_str": "1284184062",
"name": "K\u00fcrsad Celik",
"screen_name": "KrsadC",
"location": "Istanbul-Den\u0131zl\u0131",
"url": null,
"description": null,
"protected": false,
"verified": false,
"followers_count": 166,
"friends_count": 452,
"listed_count": 1,
"favourites_count": 1892,
"statuses_count": 2723,
"created_at": "Wed Mar 20 20:23:46 +0000 2013",
"utc_offset": 10800,
"time_zone": "Athens",
"geo_enabled": true,
"lang": "tr",
"contributors_enabled": false,
"is_translator": false,
"profile_background_color": "FFF04D",
"profile_background_image_url": "http:\/\/pbs.twimg.com\/profile_background_images\/378800000168605771\/dgYo8Miy.jpeg",
"profile_background_image_url_https": "https:\/\/pbs.twimg.com\/profile_background_images\/378800000168605771\/dgYo8Miy.jpeg",
"profile_background_tile": true,
"profile_link_color": "0099CC",
"profile_sidebar_border_color": "FFFFFF",
"profile_sidebar_fill_color": "F6FFD1",
"profile_text_color": "333333",
"profile_use_background_image": true,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/437899583348572160\/FamxSRwB_normal.jpeg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/437899583348572160\/FamxSRwB_normal.jpeg",
"profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/1284184062\/1393238552",
"default_profile": false,
"default_profile_image": false,
"following": null,
"follow_request_sent": null,
"notifications": null
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweeted_status": {
"created_at": "Tue Mar 31 20:39:47 +0000 2015",
"id": 583005791340404737,
"id_str": "583005791340404737",
"text": "#MehmetSelimKiraz",
"source": "\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 607600160,
"id_str": "607600160",
"name": "Bekir Develi",
"screen_name": "BekirDeveli",
"location": "\u0130stanbul\/Turkey",
"url": "http:\/\/www.bekirdeveli.com",
"description": null,
"protected": false,
"verified": false,
"followers_count": 225574,
"friends_count": 2{
"created_at": "Tue Mar 31 20:50:14 +0000 2015",
"id": 583008421152198656,
"id_str": "583008421152198656",
"text": "#mehmetselimkiraz https:\/\/t.co\/ly9MROZ6Yg",
"source": "\u003ca href=\"http:\/\/instagram.com\" rel=\"nofollow\"\u003eInstagram\u003c\/a\u003e",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 110186170,
"id_str": "110186170",
"name": "\u015eeyma Ceylan",
"screen_name": "cylnm",
"location": "ceylan",
"url": null,
"description": "\u0648\u0642\u062f\u0645 \u0643\u0644 \u0627\u0644\u0627\u0646\u0632\u0639\u0627\u062c \u0631\u0627\u062d\u0629 \u0627\u0644\u0644\u0647I http:\/\/Instagram.com\/seymaceylan_",
"protected": false,
"verified": false,
"followers_count": 1759,
"friends_count": 780,
"listed_count": 15,
"favourites_count": 7472,
"statuses_count": 9036,
"created_at": "Sun Jan 31 17:13:48 +0000 2010",
"utc_offset": -36000,
"time_zone": "Hawaii",
"geo_enabled": true,
"lang": "tr",
"contributors_enabled": false,
"is_translator": false,
"profile_background_color": "FF6699",
"profile_background_image_url": "http:\/\/pbs.twimg.com\/profile_background_images\/866467818\/24dcf5a87af2b92f9ff76fead916e4ef.jpeg",
"profile_background_image_url_https": "https:\/\/pbs.twimg.com\/profile_background_images\/866467818\/24dcf5a87af2b92f9ff76fead916e4ef.jpeg",
"profile_background_tile": true,
"profile_link_color": "9266CC",
"profile_sidebar_border_color": "FFFFFF",
"profile_sidebar_fill_color": "E5507E",
"profile_text_color": "362720",
"profile_use_background_image": true,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/574664598248419329\/Budj4Oq3_normal.jpeg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/574664598248419329\/Budj4Oq3_normal.jpeg",
"profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/110186170\/1410798690",
"default_profile": false,
"default_profile_image": false,
"following": null,
"follow_request_sent": null,
"notifications": null
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweet_count": 0,
"favorite_count": 0,
"entities": {
"hashtags": [
{
"text": "mehmetselimkiraz",
"indices": [
0,
17
]
}
],
"trends": [
],
"urls": [
{
"url": "https:\/\/t.co\/ly9MROZ6Yg",
"expanded_url": "https:\/\/instagram.com\/p\/05-6PQPQ-j\/",
"display_url": "instagram.com\/p\/05-6PQPQ-j\/",
"indices": [
18,
41
]
}
],
"user_mentions": [
],
"symbols": [
]
},
"favorited": false,
"retweeted": false,
"possibly_sensitive": true,
"filter_level": "low",
"lang": "und",
"timestamp_ms": "1427835014032"
}
You need to match individual JSON objects, and I think you can just match them and copy wherever you need using this regex:
\{(?:[^{}]+|(?0))*\}
It works in Notepad++ 6.7.5, and it will select the text from the first { up to the corresponding closing }.

how to apply conditions on JSON response in Jmeter?

Response is as follows,
I have to extract all 'deviceResponseStatus.id' where status="PENDING"
how to implement it using JSON Path or REGEX in Jmeter?
{
"apiResponseStatus": "SUCCESS",
"deviceResponseList": [
{
"apiResponseStatus": "SUCCESS",
"id": 23,
"ownership": "CORPORATE",
"deviceName": "Demimbu",
"deviceType": "MOBILE",
"osType": "ANDROID",
"inTotalStorage": 0,
"inAvailableStorage": 0,
"exTotalStorage": 0,
"exAvailableStorage": 0,
"osVersion": "4.2",
"buildVersion": "1.01.08",
"status": "PENDING",
"isDeleted": false,
"policyIdNames": {
"id": 3,
"name": "Test Policy 3"
},
"deviceGroupIdNames": {
"id": 1,
"name": "Default Group"
},
"userIdName": {
"id": 1,
"name": "Randy Lewis"
},
"lastConnected": 1423810405000,
"createdByIdName": {
"id": 1,
"name": "Randy Lewis"
},
"locked": false,
"traceOn": false,
"userEmail": "mdm.user1#gmail.com",
"enrollmentDate": 1423790255000,
"simNumbers": [
"1236547896"
],
"imeiCodes": [],
"mobileNumbers": [
"4558585858858"
],
"createDate": 1421927250000,
"lastModified": 1423810406000,
"rooted": false,
"roaming": false,
"compliant": false
},
{
"apiResponseStatus": "SUCCESS",
"id": 24,
"ownership": "CORPORATE",
"deviceName": "Photobean",
"deviceType": "MOBILE",
"osType": "ANDROID",
"inTotalStorage": 20941258752,
"inAvailableStorage": 8024924160,
"exTotalStorage": 0,
"exAvailableStorage": 0,
"bluetoothMacAddress": "8C:3A:E3:BE:C2:04",
"wirelessMacAddress": "8c:3a:e3:51:bb:e3",
"model": "LG-D686",
"osVersion": "4.4.2",
"buildVersion": "1.01.09",
"status": "PENDING",
"isDeleted": false,
"policyIdNames": {
"id": 3,
"name": "Test Policy 3"
},
"deviceGroupIdNames": {
"id": 1,
"name": "Default Group"
},
"userIdName": {
"id": 1,
"name": "Randy Lewis"
},
"make": "LGE",
"lastConnected": 1423648831000,
"createdByIdName": {
"id": 1,
"name": "Randy Lewis"
},
"locked": false,
"traceOn": false,
"userEmail": "mdm.user1#gmail.com",
"enrollmentDate": 1423628675000,
"simNumbers": [
"404909008623970"
],
"imeiCodes": [
"359004053152889",
"359004053152871"
],
"mobileNumbers": [
"638847339387"
],
"createDate": 1422435029000,
"lastModified": 1423648831000,
"rooted": true,
"roaming": false,
"compliant": false
},
{
"apiResponseStatus": "SUCCESS",
"id": 25,
"ownership": "CORPORATE",
"deviceName": "Livetube",
"deviceType": "MOBILE",
"osType": "ANDROID",
"inTotalStorage": 0,
"inAvailableStorage": 0,
"exTotalStorage": 0,
"exAvailableStorage": 0,
"osVersion": "4.2",
"buildVersion": "1.01.08",
"status": "APPROVAL_PENDING",
"isDeleted": false,
"policyIdNames": {
"id": 3,
"name": "Test Policy 3"
},
"deviceGroupIdNames": {
"id": 1,
"name": "Default Group"
},
"userIdName": {
"id": 1,
"name": "Randy Lewis"
},
"lastConnected": 1423810522000,
"createdByIdName": {
"id": 1,
"name": "Randy Lewis"
},
"locked": false,
"traceOn": false,
"userEmail": "mdm.user1#gmail.com",
"enrollmentDate": 1423790371000,
"simNumbers": [
"1236547896"
],
"imeiCodes": [],
"mobileNumbers": [
"4558585858858"
],
"createDate": 1422435030000,
"lastModified": 1423810522000,
"rooted": false,
"roaming": false,
"compliant": false
},
{
"apiResponseStatus": "SUCCESS",
"id": 26,
"ownership": "CORPORATE",
"deviceName": "Riffwire",
"deviceType": "MOBILE",
"osType": "IOS",
"inTotalStorage": 0,
"inAvailableStorage": 0,
"exTotalStorage": 0,
"exAvailableStorage": 0,
"status": "PENDING",
"isDeleted": false,
"policyIdNames": {
"id": 3,
"name": "Test Policy 3"
},
"deviceGroupIdNames": {
"id": 1,
"name": "Default Group"
},
"userIdName": {
"id": 1,
"name": "Randy Lewis"
},
"createdByIdName": {
"id": 1,
"name": "Randy Lewis"
},
"locked": false,
"traceOn": false,
"userEmail": "mdm.user1#gmail.com",
"simNumbers": [],
"imeiCodes": [],
"mobileNumbers": [],
"createDate": 1422435030000,
"lastModified": 1423216312000,
"rooted": false,
"roaming": false,
"compliant": false
},
Also, Please suggest any good option if any.
You question is JMeter specific, but i believe it's actually regular Json Path expression. For example, in SoapUI it could be done with
$.deviceResponseList[?(#.status=='PENDING')].id
Probably this could help in some way
JMeter provides JSON Path Extractor via JMeter Plugins Extras With Libs Set
I believe that this is the best way to accomplish your goal.
See Using the XPath Extractor in JMeter guide (scroll down to "Parsing JSON" chapter) for the plugin installation instructions and some sample JSON Path queries.