1

I am finding the best way to get part of JSON string instead of JTOken collection using SelectTokens(JPath).

For example :

JObject o = JObject.Parse(@"{
      'Stores': [
        'Lambton Quay',
        'Willis Street'
      ],
      'Manufacturers': [
        {
          'Name': 'Acme Co',
          'Products': [
            {
              'Name': 'Anvil',
              'Price': 50
            }
          ]
        },
        {
          'Name': 'Contoso',
          'Products': [
            {
              'Name': 'Elbow Grease',
              'Price': 99.95
            },
            {
              'Name': 'Headlight Fluid',
              'Price': 4
            }
          ]
        }
      ]
    }");

List<JToken> manufactures = o.SelectTokens("Manufacturers");

I need output JSON string instead of a collection of JToken.

Expected output :

{
    "Manufacturers": [
        {
            "Name": "Acme Co",
            "Products": [
                {
                    "Name": "Anvil",
                    "Price": 50
                }
            ]
        },
        {
            "Name": "Contoso",
            "Products": [
                {
                    "Name": "Elbow Grease",
                    "Price": 99.95
                },
                {
                    "Name": "Headlight Fluid",
                    "Price": 4
                }
            ]
        }
    ]
}

Is there any way to get output like this?

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
swapnil
  • 125
  • 11
  • Are you looking for something like `JsonExtensions.RemoveAllExcept(this JObject obj, IEnumerable paths)` from [this answer](https://stackoverflow.com/a/30333562/3744182) to [How to perform partial object serialization providing “paths” using Newtonsoft JSON.NET](https://stackoverflow.com/q/30304128/3744182)? – dbc Aug 13 '18 at 18:19

1 Answers1

2

Here are two ways to do this: You cannot convert it to exact JSON that you want in asking question, but you can extract it as it is in actual form i.e. in this case - an array. See below:

Working Example in Fiddle

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class Program
{
        public static JsonSerializer _serializer = new JsonSerializer();


    public static void Main()
    {
        JObject o = JObject.Parse(@"{
              'Stores': [
                'Lambton Quay',
                'Willis Street'
              ],
              'Manufacturers': [
                {
                  'Name': 'Acme Co',
                  'Products': [
                    {
                      'Name': 'Anvil',
                      'Price': 50
                    }
                  ]
                },
                {
                  'Name': 'Contoso',
                  'Products': [
                    {
                      'Name': 'Elbow Grease',
                      'Price': 99.95
                    },
                    {
                      'Name': 'Headlight Fluid',
                      'Price': 4
                    }
                  ]
                }
              ]
            }");

        Console.WriteLine("1. Print the key Value");
        Console.WriteLine(o["Manufacturers"].ToString());
        Console.WriteLine("--------");
        Console.WriteLine("2. Iterate and print by keyname - (Key - Value) ");

            foreach(var m in o){

                if(m.Key == "Manufacturers")
                Console.WriteLine(m.ToString());
            }


    }

}

Another option is you can hack it - extract the string using above example and then add the string between the curly braces

var json = "{" + extractedString + "}"; 
Polynomial Proton
  • 5,020
  • 20
  • 37