How do I convert json string to key\value pairs?

Gooch picture Gooch · Mar 13, 2017 · Viewed 18.3k times · Source

I want to take all the key-value pairs after the \"tags\" under "instanceData" and make them key-value pairs under "properties".

I have this...

{
  "id": "/subscriptions/1234abcd-ab12-12ab-12ab-abcdfghi1234/Daily_BRSDT_20161214_0000",
  "name": "Daily_BRSDT_20161214_0000",
  "type": "Microsoft.Commerce/UsageAggregate",
  "properties": {
    "subscriptionId": "1234abcd-ab12-12ab-12ab-abcdfghi1234",
    "usageStartTime": "2017-03-08T00:00:00+00:00",
    "usageEndTime": "2017-03-09T00:00:00+00:00",
    "meterName": "Standard IO - File Read Operation Units (in 10,000s)",
    "meterCategory": "Data Management",
    "unit": "10,000s",
    "instanceData": "{\"Microsoft.Resources\":{\"resourceUri\":\"/subscriptions/1234abcd-ab12-12ab-12ab-abcdfghi1234/resourceGroups/default-resource-group67/providers/Microsoft.Storage/storageAccounts/defaultstorage67\",\"location\":\"ussouthcentral\",\"tags\":{\"ProjectName\":\"default Portal\",\"billTo\":\"Technology\",\"component\":\"Persistant Storage\",\"department\":\"Technology\",\"displayName\":\"default Portal Storage Account\",\"enviornment\":\"default\",\"function\":\"Reporting\",\"matterNumber\":\"999999\",\"primaryowner\":\"[email protected]\",\"productLine\":\"Information Components\",\"secondaryowner\":\"[email protected]\",\"version\":\"1.0.0.0\"}}}",
    "meterId": "12345ab-259d-4206-a6ae-12345678abcd",
    "infoFields": {},
    "quantity": 0.0004
  }
}

I want this...

{
  "id": "/subscriptions/1234abcd-ab12-12ab-12ab-abcdfghi1234/Daily_BRSDT_20161214_0000",
  "name": "Daily_BRSDT_20161214_0000",
  "type": "Microsoft.Commerce/UsageAggregate",
  "properties": {
    "subscriptionId": "1234abcd-ab12-12ab-12ab-abcdfghi1234",
    "usageStartTime": "2017-03-08T00:00:00+00:00",
    "usageEndTime": "2017-03-09T00:00:00+00:00",
    "meterName": "Standard IO - File Read Operation Units (in 10,000s)",
    "meterCategory": "Data Management",
    "unit": "10,000s",
    "instanceData": "{\"Microsoft.Resources\":{\"resourceUri\":\"/subscriptions/1234abcd-ab12-12ab-12ab-abcdfghi1234/resourceGroups/default-resource-group67/providers/Microsoft.Storage/storageAccounts/defaultstorage67\",\"location\":\"ussouthcentral\"}}",
    "ProjectName":"default Portal",
    "billTo":"Technology",
    "component":"Persistant Storage",
    "department":"Technology",
    "displayName":"default Portal Storage Account",
    "enviornment":"default",
    "function":"Reporting",
    "matterNumber":"999999",
    "primaryowner":"[email protected]",
    "productLine":"Information Components",
    "secondaryowner":"[email protected]",
    "version":"1.0.0.0",
    "meterId": "12345ab-259d-4206-a6ae-12345678abcd",
    "infoFields": {},
    "quantity": 0.0004
  }
}

Is there a simple way to convert this? I am attempting to do this with RegEx with no luck.

Answer

Ryan Ternier picture Ryan Ternier · Mar 13, 2017

I would recommend looking at something like this:

How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?

Serialize List<KeyValuePair<string, string>> as JSON

Effectively you're going to need to strip out the one key you want to be parsed, and re-add that to your JSON object.

Json.NET - Newtonsoft's tool is great for working with JSON. http://www.newtonsoft.com/json

Easiest way to do it:

  1. Convert your entire JSON string to a Dictionary or to a List<KeyValuePair<string,string>>.
  2. Take the instanceData bit you want to split apart, and then parse it into another C# object.
  3. Merge both objects together using some logic to ensure no duplicate keys.
  4. Serialize your object back into JSON

This is an easy way to do it, though not the most efficient way.