0

How can I extract items from nested Json Array using Newtonsoft.Json functions or methods? I have a Json like this

{
  "Bounds": {
    "TextLength": 1379
  },
  "DocumentTypeName": "Invoice",
  "DocumentTypeField": {
    "Value": "Invoice",
    "Confidence": 1
  },
  "Fields": [
    {
      "FieldId": "RPA.DocumentUnderstanding.Invoice.LineItems",
      "FieldName": "Line Items",
      "Values": [
        {
          "Components": [
            {
              "FieldId": "RPA.DocumentUnderstanding.Invoice.LineItems.Body",
              "FieldName": "Body",
              "Values": [
                {
                  "Components": [
                    {
                      "FieldId": "RPA.DocumentUnderstanding.Invoice.LineItems.Item",
                      "FieldName": "Item",
                      "Values": [
                        {
                          "Components": [],
                          "Value": "Film 4C for the publication \"Racing World\" Visual: PNSP 02 05 Ref. 2004/021 Graphic designer honoraries 560010",
                          "Confidence": 0.962736368
                        }
                      ]
                    },
                    {
                      "FieldId": "RPA.DocumentUnderstanding.Invoice.LineItems.UnitPrice",
                      "FieldName": "Unit Price",
                      "Values": [
                        {
                          "Components": [],
                          "Value": "400.00",
                          "Confidence": 0.9779528
                        }
                      ]
                    }
                  ],
                  "Confidence": 0.9432406
                }]}],
          "Confidence": 0.920952857}]}]}

and I want to extract the red highlighted fields from it.

enter image description here

Any help will be much appreciated.

Thanks

6
  • Are you trying to do this WITHOUT deserializing the JSON object into a C# object? Commented Nov 23, 2020 at 18:28
  • @dogyear No I'm not trying to do this without deserializing. I've deserialized the JSON. Actually I'm very new in working with JSON that's why I don't have much idea about this. Commented Nov 23, 2020 at 18:42
  • This screen capture doesn't show the root of this structure. Not sure it is a collection or the path to reach these fields. Commented Nov 23, 2020 at 19:36
  • @Maryam Since you're not trying to do this without deserialization I have posted an answer about how I would normally go about it. Commented Nov 23, 2020 at 20:28
  • @derloopkat I've updated my post. Commented Nov 24, 2020 at 9:06

2 Answers 2

1

Since not deserializing is not a requirement you can do that. Just create C# object that has the exact same structure as your JSON and then do

var yourObject = JsonConvert.DeserializeObject<YourCSharpClassHere>(yourJsonString);

Then it's just a simple matter of getting the values

var fieldName = yourObject.Values[0].Components[0].Values[0].Components[0].FieldName
Sign up to request clarification or add additional context in comments.

Comments

0

You can use JSON Query

Example

var fieldNames = o.SelectTokens("Values[*].Components[*].Values[*].Components[*].FieldName");

2 Comments

Thankyou It worked. I just changed SelectToken to SelectTokens.
Welcome,Yes forgot 's' add:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.