1  
Completed
 

JSON Write "Merge" feature

I frequently have a chunk of JSON that I would like to add to another one.  I'd love to see an option to merge them without having to deconstruct one and then cycle through and add it piece by piece to the other.

 

2 replies

Here's a real-life example of what I'd like to see a Json Merge option do. I've trimmed each of the sections here to 3 items each to save space -- they actually have 6-12+ data points each.

I have various types of weather information that I collect from a variety of sources. Some I have total control over, some I'm adapting from other formats, and some I'm downloading as is. I want them all in one json file I can use with KLWP.

Here are three sections:

{"rainfall": {
    "weektot": "0.4",
    "expected": "1.899",
    "recenttot": "0.04"
  },
  "conditions": {
    "Mansfield ": {
        "Total": "",
        "New": "0.0",
        "precip": "0.00"
    },
    "home": {
        "outside": "46.6",
        "conditions": "clear",
        "inside": "51.1"
    }
  }
}


 {"forecast": {
"hourly":
{"byhour": [ "9pm, Partly Cloudy, 53° (15%)", "10pm, Partly Cloudy, 50° (15%)", "11pm, Mostly Cloudy, 48° (15%)" ], "timestamp": "1479603600000", "asof": "Saturday @ 7:48 PM", "retrieved": "1479607843020" } }}


{"forecast":{ "10day": {
"forecast":[{ "icon": "rain", "low": "38", "high": "69" }, { "icon": "snow", "low": "27", "high": "38" }, {
"icon": "snow", "low": "24", "high": "33" }], "timestamp": "1479602400000", "asof": "Saturday @ 7:40 PM", "retrieved": "1479605643020" }}}


I want to end up with somethimg like this:

{"weather": { "rainfall": {"weektot": "etc."}, "conditions": { "Mansfield ": {"Total": "etc."}, "home": {"outside": "etc."} }, "forecast": { "hourly": {
"byhour":["etc."], "timestamp": "etc."
}, "10day": {
"forecast":[{"icon": "etc."}, {"icon": "etc."}], "timestamp": "etc."
}
} }}

 

I'm picturing that we would provide an optional base chunk, the path to the object we wanted the others added to, and the other chunks. In this example, there isn't a base chunk, so the object (weather) would be created empty and the chunks added to it. Chunks with the same top-level object (like my two forecasts) would get combined. Maybe we could only merge in one chunk at a time, and use the result as the base chunk for the next round.

Currently, combining complex chunks requires reading each part out into variables and then writing them back. This way wouldn't require that double conversion and wouldn't require us to know as much about the structure of each part.

s

Wouldn't something like this work to merge your 2nd and 3rd json for example?

 

Test (2)
A1: Variable Set [ Name:%json To:{"forecast": {
"hourly":
{"byhour": [
"9pm, Partly Cloudy, 53° (15%)",
"10pm, Partly Cloudy, 50° (15%)",
"11pm, Mostly Cloudy, 48° (15%)"
],
"timestamp": "1479603600000",
"asof": "Saturday @ 7:48 PM",
"retrieved": "1479607843020"
}
}} Do Maths:Off Append:Off ]
A2: AutoTools Json Write [ Configuration:Separator: ####
Json Input: %json
Json Keys: 10day
Json Values: {
"forecast":[{
"icon": "rain",
"low": "38",
"high": "69"
}, {
"icon": "snow",
"low": "27",
"high": "38"
}, {
"icon": "snow",
"low": "24",
"high": "33"
}],
"timestamp": "1479602400000",
"asof": "Saturday @ 7:40 PM",
"retrieved": "1479605643020"
}
Prettify: true
Arrays Separator: | Timeout (Seconds):60 ]
A3: Flash [ Text:%atjsonresult Long:Off ]

 

I think this is what you need right?

Oh, I see.  Use a separator that doesn't exist in the data.  Hadn't thought of that.

s

yes, exactly :) I'll mark this as done. Let me know if I shouldn't for some reason.

s

Can you give me a specific example of 2 JSON chunks you'd want to merge? Thanks!