-2

With the following json:

{
    "operationalLayers": [
        {
            "id": "GRSM_STORM_TOTAL_PRECIP_8083",
            "layerType": "ArcGISFeatureLayer",
            "url": "https://services1.arcgis.com/fBc8EJBxQRMcHlei/arcgis/rest/services/GRSM_STORM_TOTAL_PRECIP/FeatureServer/0",
            "visibility": true,
            "opacity": 0.8,
            "title": "GRSM_STORM_TOTAL_PRECIP",
            "itemId": "20370bddfa7948bb98cf5f9e143b44b2",
            "layerDefinition": {
                "drawingInfo": {
                    "renderer": {
                        "visualVariables": [
                            {
                                "type": "colorInfo",
                                "field": "INCHES",
                                "valueExpression": null,
                                "stops": [
                                    {
                                        "value": 0.753,
                                        "color": [
                                            255,
                                            252,
                                            212,
                                            255
                                        ],
                                        "label": "< 0.7"
                                    },
                                    {
                                        "value": 2.91475,
                                        "color": [
                                            238,
                                            190,
                                            139,
                                            255
                                        ],
                                        "label": null
                                    },
                                    {
                                        "value": 5.0765,
                                        "color": [
                                            221,
                                            128,
                                            66,
                                            255
                                        ],
                                        "label": "5.07"
                                    },
                                    {
                                        "value": 7.238250000000001,
                                        "color": [
                                            162,
                                            64,
                                            33,
                                            255
                                        ],
                                        "label": null
                                    },
                                    {
                                        "value": 9.4,
                                        "color": [
                                            102,
                                            0,
                                            0,
                                            255
                                        ],
                                        "label": "> 9.3"
                                    }
                                ]
                            },
                            {
                                "type": "sizeInfo",
                                "target": "outline",
                                "expression": "view.scale",
                                "valueExpression": "$view.scale",
                                "stops": [
                                    {
                                        "size": 1.5,
                                        "value": 13459
                                    },
                                    {
                                        "size": 0.75,
                                        "value": 42058
                                    },
                                    {
                                        "size": 0.375,
                                        "value": 168232
                                    },
                                    {
                                        "size": 0,
                                        "value": 336463
                                    }
                                ]
                            }
                        ],
                        "authoringInfo": {
                            "visualVariables": [
                                {
                                    "type": "colorInfo",
                                    "minSliderValue": 0.753,
                                    "maxSliderValue": 9.4,
                                    "theme": "high-to-low"
                                }
                            ]
                        },
                        "type": "classBreaks",
                        "field": "INCHES",
                        "minValue": -9007199254740991,
                        "classBreakInfos": [
                            {
                                "symbol": {
                                    "color": [
                                        170,
                                        170,
                                        170,
                                        255
                                    ],
                                    "outline": {
                                        "color": null,
                                        "width": 0.75,
                                        "type": "esriSLS",
                                        "style": "esriSLSNull"
                                    },
                                    "type": "esriSFS",
                                    "style": "esriSFSSolid"
                                },
                                "classMaxValue": 9007199254740991
                            }
                        ]
                    }
                }
            },
            "popupInfo": {
                "title": "GRSM_STORM_TOTAL_PRECIP: {Id}",
                "fieldInfos": [
                    {
                        "fieldName": "OBJECTID",
                        "label": "OBJECTID",
                        "isEditable": false,
                        "tooltip": "",
                        "visible": false,
                        "stringFieldOption": "textbox"
                    },
                    {
                        "fieldName": "Id",
                        "label": "Id",
                        "isEditable": true,
                        "tooltip": "",
                        "visible": true,
                        "stringFieldOption": "textbox",
                        "format": {
                            "places": 0,
                            "digitSeparator": true
                        }
                    },
                    {
                        "fieldName": "gridcode",
                        "label": "gridcode",
                        "isEditable": true,
                        "tooltip": "",
                        "visible": true,
                        "stringFieldOption": "textbox",
                        "format": {
                            "places": 0,
                            "digitSeparator": true
                        }
                    },
                    {
                        "fieldName": "INCHES",
                        "label": "INCHES",
                        "isEditable": true,
                        "tooltip": "",
                        "visible": true,
                        "stringFieldOption": "textbox",
                        "format": {
                            "places": 2,
                            "digitSeparator": true
                        }
                    },
                    {
                        "fieldName": "TIMESTAMP",
                        "label": "TIMESTAMP",
                        "isEditable": true,
                        "tooltip": "",
                        "visible": true,
                        "stringFieldOption": "textbox",
                        "format": {
                            "dateFormat": "shortDateShortTime"
                        }
                    },
                    {
                        "fieldName": "Shape__Area",
                        "label": "Shape__Area",
                        "isEditable": false,
                        "tooltip": "",
                        "visible": false,
                        "stringFieldOption": "textbox",
                        "format": {
                            "places": 2,
                            "digitSeparator": true
                        }
                    },
                    {
                        "fieldName": "Shape__Length",
                        "label": "Shape__Length",
                        "isEditable": false,
                        "tooltip": "",
                        "visible": false,
                        "stringFieldOption": "textbox",
                        "format": {
                            "places": 2,
                            "digitSeparator": true
                        }
                    }
                ],
                "description": null,
                "showAttachments": true,
                "mediaInfos": []
            }
        }
    ],
    "baseMap": {
        "baseMapLayers": [
            {
                "id": "World_Hillshade_3689",
                "layerType": "ArcGISTiledMapServiceLayer",
                "url": "https://services.arcgisonline.com/arcgis/rest/services/Elevation/World_Hillshade/MapServer",
                "visibility": true,
                "opacity": 1,
                "title": "World Hillshade"
            },
            {
                "id": "VectorTile_6451",
                "type": "VectorTileLayer",
                "layerType": "VectorTileLayer",
                "title": "World Topographic Map",
                "styleUrl": "https://www.arcgis.com/sharing/rest/content/items/7dc6cea0b1764a1f9af2e679f642f0f5/resources/styles/root.json",
                "itemId": "7dc6cea0b1764a1f9af2e679f642f0f5",
                "visibility": true,
                "opacity": 1
            }
        ],
        "title": "Topographic"
    },
    "spatialReference": {
        "wkid": 102100,
        "latestWkid": 3857
    },
    "authoringApp": "WebMapViewer",
    "authoringAppVersion": "7.4",
    "version": "2.16"
}

I need to replace some values in

                "visualVariables": [
                    {
                        "type": "colorInfo",
                        "field": "INCHES",
                        "valueExpression": null,
                        "stops": [
                            {
                                "value": 0.753,
                                "color": [
                                    255,
                                    252,
                                    212,
                                    255
                                ],
                                "label": "< 0.7"
                            },
                            {
                                "value": 2.91475,
                                "color": [
                                    238,
                                    190,
                                    139,
                                    255
                                ],
                                "label": null
                            },
                            {
                                "value": 5.0765,
                                "color": [
                                    221,
                                    128,
                                    66,
                                    255
                                ],
                                "label": "5.07"
                            },
                            {
                                "value": 7.238250000000001,
                                "color": [
                                    162,
                                    64,
                                    33,
                                    255
                                ],
                                "label": null
                            },
                            {
                                "value": 9.4,
                                "color": [
                                    102,
                                    0,
                                    0,
                                    255
                                ],
                                "label": "> 9.3"
                            }
                        ]
                    }

so that for example the output is

                "visualVariables": [
                    {
                        "type": "colorInfo",
                        "field": "INCHES",
                        "valueExpression": null,
                        "stops": [
                            {
                                "value": 0.5,
                                "color": [
                                    255,
                                    252,
                                    212,
                                    255
                                ],
                                "label": "< 0.5"
                            },
                            {
                                "value": 5,
                                "color": [
                                    238,
                                    190,
                                    139,
                                    255
                                ],
                                "label": 5
                            },
                            {
                                "value": 15,
                                "color": [
                                    221,
                                    128,
                                    66,
                                    255
                                ],
                                "label": "15"
                            },
                            {
                                "value": 10,
                                "color": [
                                    162,
                                    64,
                                    33,
                                    255
                                ],
                                "label": 10
                            },
                            {
                                "value": 20,
                                "color": [
                                    102,
                                    0,
                                    0,
                                    255
                                ],
                                "label": "> 20"
                            }
                        ]
                    }

So far all I've managed to do is read the value one by one for example

import json
from pprint import pprint
with open('C:/Temp/NEXRAD_PYTHON/new 1.json') as f:
    data = json.load(f)
data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["value"]
5.0765
data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["label"]
'5.07'

but what I'm looking to do is have a separate (list?dictionary), for example

['.5', '5', '10', '15', '20']

which is updated outside of this workflow (every 10 minutes the number will be changed as the result of another program) that will replace "value" and "label".

I've looked at Change json.dumps dictionary values in python, Replace multiple keys and values of JSON file in Python, Python - change JSON values and pretty print, and https://www.geeksforgeeks.org/python-ways-to-change-keys-in-dictionary/

Which either don't match my situation or I'm missing something obvious.

tpcolson
  • 716
  • 1
  • 11
  • 27

1 Answers1

0

...and just like that. Added a random # generator to test actual values.

import json
from pprint import pprint
import numpy as np
import random
min = random.uniform(.01,1)
max = random.uniform(5,15)
a = np.array([min,max])
x = np.percentile(a, 25)
y = np.percentile(a, 50)
z = np.percentile(a, 75)
print (x,y,z)
with open('C:/Temp/NEXRAD_PYTHON/new 1.json','r') as f:
    data = json.load(f)



with open('C:/Temp/NEXRAD_PYTHON/new 1.json','w') as f:

    data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][0]["value"] = min
    data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][1]["value"] = x
    data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["value"] = y
    data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][3]["value"] = z
    data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][4]["value"] = max
    data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][0]["label"] = "<"+str(round(min,2))
    data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][1]["label"] = str(round(x,2))
    data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["label"] = str(round(y,2))
    data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][3]["label"] = str(round(z,2))
    data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][4]["label"] = ">"+str(round(max,2))
    data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["authoringInfo"]["visualVariables"][0]["minSliderValue"] = round(min,3)
    data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["authoringInfo"]["visualVariables"][0]["maxSliderValue"] = round(max,3)
    f.write(json.dumps(data))


f.close()
tpcolson
  • 716
  • 1
  • 11
  • 27