# \[Office 365\] Help parse json with key | value in data

**URL:** <https://discuss.elastic.co/t/office-365-help-parse-json-with-key-value-in-data/125850>\
**Category:** Logstash\
**Created:** [March 28, 2018, 4:32am UTC](https://discuss.elastic.co/t/office-365-help-parse-json-with-key-value-in-data/125850 "2018-03-28T04:32:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tatdat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tatdat/32/113160_2.png) [@tatdat](https://discuss.elastic.co/u/tatdat)\
**Post date:** [March 28, 2018, 4:32am UTC](https://discuss.elastic.co/t/office-365-help-parse-json-with-key-value-in-data/125850/1 "2018-03-28T04:32:40Z")

</div>

Im working on log of O365 via O365 API.

I have sample log look like

```
"o365": {
"ExtendedProperties": [
        {
          "Name": "UserAgent",
          "Value": "Microsoft Office/15.0 (Windows NT 6.2; Microsoft Outlook 15.0.4420; Pro)"
        },
        {
          "Name": "UserAuthenticationMethod",
          "Value": "1"
        },
        {
          "Name": "RequestType",
          "Value": "OrgIdWsTrust2:process"
        },
        {
          "Name": "ResultStatusDetail",
          "Value": "Success"
        }
      ]
}

```

Now i want data become

```
{
  "o365": {
    "ExtendedProperties": {
      "UserAgent": "Microsoft Office/15.0 (Windows NT 6.2; Microsoft Outlook 15.0.4420; Pro)",
      "UserAuthenticationMethod": "1",
      "RequestType": "OrgIdWsTrust2:process",
      "ResultStatusDetail": "Success"
    }
  }
}

```

Someone tell me that should be use ruby filter, but i dont know how (i dont know ruby program).  
Can someone help me finish parse log Office 365 ?  
P/s: The number of elements in the array ( **ExtendedProperties** ) is unknown - maybe have 3 or 5 or 7 elements.

Thanks so much!

---

<div class="post-metadata">

**Author:** ![tatdat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tatdat/32/113160_2.png) [@tatdat](https://discuss.elastic.co/u/tatdat)\
**Post date:** [April 6, 2018, 3:48am UTC](https://discuss.elastic.co/t/office-365-help-parse-json-with-key-value-in-data/125850/2 "2018-04-06T03:48:06Z")

</div>

I tried this filter but not work

```
ruby {
      init => "
        def arrays_to_hash(h)
          h.each do |k,v|            
            value = v || k
            if value.is_a?(Array)
                value_hash = {}
                value.each_with_index do |v, i|
                    value_hash[i.to_s] = v
                end
                h[k] = value_hash
            end
            if value.is_a?(Hash) || value.is_a?(Array)
              arrays_to_hash(value)
            end
          end
        end
      "
      code => "arrays_to_hash([o365][ExtendedProperties])"
    }

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/2/a2e05468e0592ee83319c4262a3d325b5dd2c348.jpg)  
Someone help me ?

---

<div class="post-metadata">

**Author:** ![tudor](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tudor/32/3753_2.png) [@tudor](https://discuss.elastic.co/u/tudor)\
**Post date:** [April 6, 2018, 9:01pm UTC](https://discuss.elastic.co/t/office-365-help-parse-json-with-key-value-in-data/125850/3 "2018-04-06T21:01:00Z")

</div>

The `"arrays_to_hash([o365][ExtendedProperties])"` line seems wrong. Try `"arrays_to_hash(event.get('o365').get('ExtendedProperties')"`. I didn't check the rest of the code, though.

---

<div class="post-metadata">

**Author:** ![tatdat](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tatdat/32/113160_2.png) [@tatdat](https://discuss.elastic.co/u/tatdat)\
**Post date:** [April 9, 2018, 7:35am UTC](https://discuss.elastic.co/t/office-365-help-parse-json-with-key-value-in-data/125850/4 "2018-04-09T07:35:18Z")

</div>

Thank @tudor but sth wrong in synstax ?

![image](https://us1.discourse-cdn.com/elastic/original/3X/d/3/d3d0545db27fe8190bdb0dd50a1386319aed385a.jpg)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 7, 2018, 7:35am UTC](https://discuss.elastic.co/t/office-365-help-parse-json-with-key-value-in-data/125850/5 "2018-05-07T07:35:45Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
