0

I'm currently on a somewhat difficult problem to handle. Let's have some background first:

My program uses JSON (using Newtonsoft.JSON) to transfer data between plugins. To make this data readable between plugins, I have a table in the background, which contains the field names of both plugins, essentially matching them.

In the plugins themselves, you have to define fields you want to expose to my program. This is done via a CSV file, containing 2 columns, which again, matches 2 fields, the field in the data source and the field, that's gonna be exposed (see table below). Essentially, I want to pull data from incoming JSON and expose them dynamically using this table:

+-------------------+---------------------+
|   Source Field    | Field in my program |
+-------------------+---------------------+
| mainDetail.number | Article ID          |
| name              | Article Name        |
| supplier          | Supplier            |
+-------------------+---------------------+

From this JSON:

{
    "name": "Bread",
    "supplier": "That Bread Company",
    "mainDetail": {
        "number": 10000
    }
}

In this case, the source field is supposed to match incoming JSON. The issue is, my current implementation can only cover objects in said JSON. It does this, by resolving the source field using the SelectToken(string) method, which works well for objects. Here an example of how the resulting JSON looks like:

{
    "Article Name": "Bread",
    "Supplier": "That Bread Company",
    "Article ID": 10000
}

But, the JSON this plugin can receive also contains some arrays, where the length is unknown. So using my current implementation, I'd need to have some wildcard, which matches for all elements of an array and renaming those as well, however, I haven't come across a wildcard like that yet.

Here's the same example, but with an array and how I want it to look like:

The table I want to use (with a pseudo-wildcard):

+--------------------------+---------------------+
|       Source Field       | Field in my program |
+--------------------------+---------------------+
| mainDetail.number        | Article ID          |
| name                     | Article Name        |
| supplier                 | Supplier            |
| mainDetail.prices        | Prices              |
| mainDetail.prices[?].usd | Price in USD        |
+--------------------------+---------------------+

The incoming JSON:

{
    "name": "Bread",
    "supplier": "That Bread Company",
    "mainDetail": {
        "number": 10000,
        "prices": [
            {
                "usd": 199.99
            },
            {
                "usd": 159.99
            }
        ]
    }
}

And how I want it to look like:

{
    "Article Name": "Bread",
    "Supplier": "That Bread Company",
    "Article ID": 10000,
    "Prices": [
        {
            "Price in USD": 199.99
        },
        {
            "Price in USD": 159.99
        }
    }
}

On the documentation, I found a way to query using JSONPath, however it doesn't do a good job explaining it. So I have no idea how to actually use it and if it can do what I want it to do.

What I want to achieve with this, is a dynamic way of accessing JSON without rewriting the parsing part, making it flexible to sudden needs of certain fields.

Does anyone have an approach I could try?

  • Are you looking for the JSONPath wildcard `*` as shown in, say, [Testing for nested keys with Json.NET?](https://stackoverflow.com/a/39337136/3744182)? – dbc Apr 16 '18 at 09:40
  • Huh, that seems to be a step closer to what I want to do. Thank you, I'll give an update here, if it works. – Jean Luc Nürrenberg Apr 16 '18 at 09:49

0 Answers0