Home > Mobile >  How to search a JSON hash using a Regex?
How to search a JSON hash using a Regex?

Time:11-22

I'm using an API to return all Macros to me, I am trying to return all the "macros" where "actions" contains a "value" matching my Regexp Pattern which I will link below.

I've tried below and other methods, but it returns me nil for present values. Any tips appreciated

macros["value"].select { |m| m['key'] == 'value' }.first['/^DE([0-9a-zA-Z]\s?){20}$/gm']

API result snippet:

   jsObj
=> {"macros"=>
  [{"url"=>"https://s/1900002708354.json",
    "id"=>1900002708354,
    "title"=>"Append Signature",
    "active"=>true,
    "updated_at"=>"2021-10-22T14:11:15Z",
    "created_at"=>"2021-10-22T14:11:15Z",
    "position"=>10001,
    "description"=>"This macro appends a signature to the message ",
    "actions"=>[{"field"=>"comment_value_html", "value"=>"<p>Mit besten Grüßen,</p><p>{{current_user.name}} [{{ticket.account}}]&nbsp; <br></p><p><br></p><p>{{dc.signature_email}}<br></p><p><br></p>"}],
    "restriction"=>nil},
   {"url"=>"949.json",
    "id"=>59071949,
    "title"=>"information",
    "description"=>nil,
    "actions"=>[{"field"=>"priority", "value"=>"low"}, {"field"=>"comment_value", "value"=>"DE89370400440532013000" "DE89 3704

 0044 0532 0130 00"
"}],
        "restriction"=>nil},

Desired Result:

  {
        "macros": [
            {
                "url": "x.json",
                "id": 1900002708354,
                "actions": [
                    {
                        "field": "comment_value_html",
                        "value": "DE89 3704 0044 0532 0130 00"
                    }
                ],
                "restriction": null
            },
 
              {
                "url": "x.json",
                "id": 59071949,
                "actions": [
                    {
                        "field": "priority",
                        "value": "low"
                    },
                    {
                        "field": "comment_value",
                        "value": "DE89 3704 0044 0532 0130 00
"
                    }
                ],
                "restriction": null
            },

CodePudding user response:

Given that macros is the JSON object containing the macros data, you can use

macros.select { |m| m["actions"].any? { |w| /\ADE(?:[0-9a-zA-Z]\s?){20}\z/.match?(w["value"]) } }

Here is a Ruby demo:

require 'json'

j = <<-DATA
{
    "macros": [
        {
            "url": "x.json",
            "id": 1900002708354,
            "actions": [
                {
                    "field": "comment_value_html",
                    "value": "DE11111111111222222220"
                }
            ],
            "restriction": null
        },
        {
            "url": "x.json",
            "id": 59071949,
            "actions": [
                {
                    "field": "priority",
                    "value": "low"
                },
                {
                    "field": "comment_value",
                    "value": "DE12345678901234567890"
                }
            ],
            "restriction": null
        }
]}
DATA

jsObj = JSON.parse(j)
macros = jsObj['macros']
puts jsObj['macros'].select { |m| m["actions"].any? { |w| /\ADE(?:[0-9a-zA-Z]\s?){20}\z/.match?(w["value"]) } }

Output:

{"url"=>"x.json", "id"=>1900002708354, "actions"=>[{"field"=>"comment_value_html", "value"=>"DE11111111111222222220"}], "restriction"=>nil}
{"url"=>"x.json", "id"=>59071949, "actions"=>[{"field"=>"priority", "value"=>"low"}, {"field"=>"comment_value", "value"=>"DE12345678901234567890"}], "restriction"=>nil}

The .select { |m| m["actions"].any? { |w| /\ADE(?:[0-9a-zA-Z]\s?){20}\z/.match?(w["value"]) } } main part gets all actions nodes that contain an array with a value key whose value matches the regex given.

  • Related