Newtonsoft.Json icon indicating copy to clipboard operation
Newtonsoft.Json copied to clipboard

Want to parse big JSON file to specific keys as json

Open spineshanks opened this issue 2 years ago • 3 comments

Hello Team,

I have a JSON file { "name": "James", "hobbies": [ ".NET", "Blogging", "Reading", "Xbox", "LOLCATS" ], "age": 32, "City": "Wales", "Country": "UK" }

I want to extract only the name and City as output JSON. any idea how can I achieve this. this is just sample JSON but has very big JSON files from which we have to extract only selected fields(configurable from DB, so plan to store schema of output JSON).

any help is appreciated.

Thanks

spineshanks avatar May 22 '22 10:05 spineshanks

yeah, creating a model can be one approach. But I want to make it configurable with no code changes if someone wants new fields from JSON. so looking more toward saving the schema or keys which can come out as a subset of input JSON files.

spineshanks avatar May 22 '22 11:05 spineshanks

It depends.

Start with trying to use the classes offered in Newtonsoft.Json.Linq (JArray, JObject, JValue, etc..), and read your json data into such a JArray/JObject/JValue representation. This can then be processed further, i.e., finding the objects and properties and values of interest (either by walking through the JArray/JObject/JValue representation or by using JsonPath/JPath selectors) the and manipulating the JArray/JObject/JValue representation or building a new one with the data of interest. (respective section in the Newtonsoft.Json doc: https://www.newtonsoft.com/json/help/html/LINQtoJSON.htm)

If the json file size becomes a real problem for you (because deserializing into a JArray/JObject/JValue will deserialize the complete json data), you can instead "manually" read the json data in a forward fashion using a JsonTextReader directly. (doc: https://www.newtonsoft.com/json/help/html/ReadingWritingJSON.htm). Depending on where and how deeply the json properties of interest are located within the json data structure, this might be simple or a little more complicated, though. With the data obtained, you could then either use a JsonTextWriter to write the output Json, or create a new JArray/JObject/JValue representation that can be easily written to a json file/string.

By the way, if there are further questions about how to use JArray/JObject/JValue or JsonTextReader appropriately for your application scenario, don't hesitate to ask on stackoverflow.com. Unlike the issue tracker here (which puts an emphasis on issues the library is having), stackoverflow.com not only is a Q&A site specifically focused on how to do things, but your question will also be seen by many more eyes than here on the issue tracker.

(P.S.: I deleted my previous comment, as i indeed missed the part of the fields of interest being configurable.)

elgonzo avatar May 22 '22 11:05 elgonzo

I can think of one solution that will take input JSON and pass it to the schema(only required fields). That would give me output JSON. something similar to Parse JSON from Powerautomate connector.https://techcommunity.microsoft.com/t5/microsoft-365-pnp-blog/how-to-use-parse-json-action-in-power-automate/ba-p/2121861 image

spineshanks avatar May 22 '22 11:05 spineshanks