Hi

I am new to using Nim and having problems trying to access a JObject's values 
(ie JString; JInt; JFloat; etc) when those values are stored within a JOBject, 
inside the main JsonNode JObject.

I can confirm the values exist with a contains call on the JObject, but can 
figure out how to then access the values themsleves. I believe the JObject is a 
table with a string (key?) and value (JString; JInt; etc). I am not clear on 
the syntax or correct proc to use to gain direct access to them.

I have found the proc getFields() but cant figure out how to use it correctly 
either.

All the examples online I can find just show accessing flat JSON files, I can 
not find any that show how to access JOjects when they are within the main 
JsonNode JObject itself.

Is there a direct way to access them please?

Thanks for any help!

Simon
    
    
    import json
    
    let jsonInput = """
    {
        "lat": 56.419212,
        "long": -5.291481,
        "timezone": "Europe/London",
        "currently": {
                "measure": 1,
                "summary": "Windy and Overcast",
                },
        "minutely": {
                "summary": "Windy and overcast for the hour.",
                "measure": 2,
                "data": [{
                            "time": 1575822000,
                            "precipIntensity":      0,
                            "precipProbability":    0
                        },{
                            "time": 1575822000,
                            "precipIntensity":      0,
                            "precipProbability":    0
                        }],
                }
    }
    """
    
    var jsonData: JsonNode
    
    # read the jsonInput into the JObject called jsonData
    echo "Parsing JSON data..."
    try:
      jsonData = parseJson(jsonInput)
      echo "Parse completed!"
    except JsonParsingError:
      let
        e = getCurrentException()
        msg = getCurrentExceptionMsg()
      echo "Got JSON exception ", repr(e), " with message ", msg
    except:
      echo "Unknown exception!"
    
    # dump all the jsonData JObject - so prints all its keys and values:
    echo "\nThe JSON data parsed is:"
    if jsonData.kind == JObject:
      for key, value in pairs(jsonData):
        echo "Key: ", key, " with value: ", value
      echo "done"
    else:
      echo "No 'jsonData' JOject exist"
    
    doAssert jsonData.kind == JObject
    doAssert jsonData["lat"].getFloat() == 56.419212
    doAssert jsonData["timezone"].getStr() == "Europe/London"
    
    doAssert jsonData["currently"].kind == JObject
    doAssert jsonData["currently"].contains("summary") == true
    
    # STUCK HERE : how to you access the JOject values directly??
    #doAssert jsonData["currently"].getStr("summary") == "Windy and Overcast"
    #doAssert jsonData["currently"["summary"]].getStr() == "Windy and Overcast"
    
    # tried this too
    #let currentlyData = getFields(jsonData["currently"])
    #echo jsonData["currentlyData"].getStr("summary")
    
    # Can get them this way - but is there a direct approach?
    for key, value in pairs(jsonData["currently"]):
      echo "Key: '", key, "' : ", value
    echo "done loop"
    
    echo "Done"
    
    
    Run

Reply via email to