magic-chunks / magic-chunks-dotnetcore

Easy to use tool to config transformations for JSON, XML and YAML.
MIT License
106 stars 48 forks source link

Array Support with JSONs #67

Open husainaloos opened 6 years ago

husainaloos commented 6 years ago

Does this tasks support replacing an item in a JSON array in configuration?

Suppose I have this json:

{
    "my_array" : [
        {"value" : 1},
        {"value" : 2}
    ]
}

and I want to transform it to

{
    "my_array" : [
        {"value" : 1},
        {"value" : 5}
    ]
}

what would be the path to do that?

Sigman32 commented 6 years ago

replacing current array values is not problem.

my_array[1]/@value

however, if item does not exist, you have to explicitly add it using:

my_array[]`1/@value

leechdraw commented 6 years ago

@sergeyzwezdin , hi! Is there any way to do this:

  1. We have json with next structure:
    [
    {
    "field1": "value1",
    "field2": "value2"
    }  ....
    ]
  2. We need to add one more item to array:
    {
    "field1": "value3",
    "field2": "value4"
    }  
gpin1120 commented 6 years ago

@Sigman32 Thanks for sharing! I have some questions regarding this,

I have a simple array like this in my configs

"my_array": [ "value1", "value2" ], In the transform task I added this "my_array[0]" : "value1" "my_array[1]" : "value2"

The end result is a new entry at the end of the config exactly like the following, seems like the task isn't evaluating it as an array but rather as strings "my_array[0]" and "my_array[1]" "my_array[0]" : "value1" "my_array[1]" : "value2"

If I do this however, it adds new additional items in the array without issues "my_array[]" : "value1" "my_array[]" : "value2"

Do you have any ideas what I might be doing wrong? Thank you. :)

eavonius commented 6 years ago

This doesn't seem to work. I'm trying to replace the following at the bottom of this JSON:

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.ApplicationInsights"
    ],
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp} [{Level}] {Message} {Exception} {Properties} {NewLine}",
          "theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Literate, Serilog.Sinks.Console"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": ".\\Logs\\ApiService.log",
          "fileSizeLimitBytes": 10000000
        }
      },
      {
        "Name": "ApplicationInsights",
        "Args": {
          "logName": "ApiService",
          "instrumentationKey": "<THIS IS WHAT I WANT TO REPLACE>",
          ...

I use this path:

Serilog/WriteTo[2]/Args/instrumentationKey

But it just creates a new "WriteTo[2]" section below Serilog - it doesn't replace the existing second item (the one for Azure App Services).

Patryx commented 5 years ago

"my_array": [ "value1", "value2" ],

So how can we change these settings?

grazumkov commented 4 years ago

Workaround.

.NET Configuration supports different syntax. see: Configuration in ASP.NET Core

For example, you can define multiple WriteTo sections with index:

"Serilog": {
   "WriteTo:0": {
    "Name": "Console",
    "Args": {
            }
        },
    "WriteTo:1":{
    "Name": "File",
    "Args": {
            "path": "./logs/log-.log",
        }
    },
}

and use path: Serilog/WriteTo:1/Args/path

Lekiniu commented 3 years ago

replacing current array values is not problem.

my_array[1]/@value

however, if item does not exist, you have to explicitly add it using:

my_array[]`1/@value

I need to add new array element in appsetting, but this does not work