0

After an API request I get a JSON file(parent) with some URLs to other JSON files(children). Is it possible to download all files and convert the children files to JSON objects in a parent file?

It is a ASP.Net Core 3.1 application written in C#.

Example Input:

[
    {
        "title": "AAA",
        "dates": [
            {
                "a_date": "24.09.2021",              
                "push": false,
                "date_url": "https://www-api.aaa.com/api/HEUFCCEWJHB=.json"
            },
            {
                "a_date": "27.09.2021",              
                "push": false,
                "date_url": "https://www-api.aaa.com/api/VFEWVWEWXS=.json"
            }
        ]
    },
    {
        "title": "BBB",
        "dates": [
            {
                "a_date": "24.10.2021",              
                "push": false,
                "date_url": "https://www-api.aaa.com/api/HBTFDECEC=.json"
            },
            {
                "a_date": "27.10.2021",              
                "push": false,
                "date_url": "https://www-api.aaa.com/api/EWFEWIFWEW=.json"
            }
        ]
    }
]

Expected Output:

    [
    {
        "title": "AAA",
        "dates": [
            {
                "a_date": "24.09.2021",              
                "push": false,
                "date_url": [
                    {
                        "aa": "xx",              
                        "bb": false,
                    },
                    {
                        "aa": "zzz",              
                        "cc": true,
                    }
                ]
            },
            {
                "a_date": "27.09.2021",              
                "push": false,
                "date_url": [
                    {
                        "aa": "xx",              
                        "bb": false,
                    },
                    {
                        "aa": "zzz",              
                        "cc": true,
                    }
                ]
            }
        ]
    },
    {
        "title": "BBB "
        ...
    }
]

Optionaly:

At the end the parent JSON should be splitted in different JSON files by title.

5
  • Do you want it using Newtonsoft.Json or System.Text.Json? Commented Jul 15, 2021 at 17:25
  • Also, how should the web requests be made? Are using a particular method, do you need any custom headers? Commented Jul 15, 2021 at 17:27
  • What does your "aa","bb","cc" mean in your Expected Output? Commented Jul 16, 2021 at 3:17
  • @trinalbadger587 I prefer Newtonsoft.Json. Ther is a simple http request any custom headers requred. Commented Jul 27, 2021 at 6:44
  • @YiyiYou it is just a sample of the child-data Commented Jul 27, 2021 at 6:45

1 Answer 1

1

You can try this converter:

public class UrlDownloaderConverter : JsonConverter<object>
{
    public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        => JsonSerializer.Deserialize(DownloadJson(reader.GetString()), typeToConvert, options);

    public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
        => JsonSerializer.Serialize(writer, value, value.GetType(), options);

    public static string DownloadJson(string url)
    {
        using (HttpClient httpClient = new HttpClient())
        using (HttpResponseMessage msg = httpClient.GetAsync(url).Result)
        using (HttpContent content = msg.Content)
            return content.ReadAsStringAsync().Result;
    }
}

Example Usage

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer. There is an other problem: the object-names in child-JSONs are different, so i can not map it to a Model.
@AlexanderS., I do not know what you mean. Also, I do not know Newtonsoft.Json so I can't help you with that.
There can be hundreds of other names in place of "aa" "bb" "cc". I can't add them all in a model.
@AlexanderS., you will have to ask that in a separate question as I do not know the context but from just what you told me I have no idea. You will probably have to write a custom converter to convert it to the object you want.
Ok. Thank you for your help! Now I have an idea how I can solve ist. May be I will post it later.

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.