Python: create json query - json

I'm trying to get python to create a json formatted like :
[
{
"machine_working": true
},
{
"MachineName": "TBL165-169",
"MachineType": "Rig Test"
}
]
However, i can seam to do it, this is the code i have currently but its giving me error
this_is_a_dict_too=[]
this_is_a_dict_too = dict(State="on",dict(MachineType="machinetype1",MachineName="MachineType2"))
File "c:\printjson.py", line 40
this_is_a_dict_too = dict(Statedsf="test",dict(MachineType="Rig Test",MachineName="TBL165-169")) SyntaxError: non-keyword arg after
keyword arg

this_is_a_dict_too = [dict(machine_working=True),dict(MachineType="machinetype1",MachineName="MachineType2")]
print(this_is_a_dict_too)

You are trying to make dictionary in dictionary, the error message say that you try to add element without name (corresponding key)
dict(a='b', b=dict(state='on'))
will work, but
dict(a='b', dict(state='on'))
won't.
The thing that you presented is list, so you can use
list((dict(a='b'), dict(b='a')))
Note that example above use two dictionaries packed into tuple.
or
[ dict(a='b'), dict(b='a') ]

Related

escape forward slash in jsonpath

I'm trying to write a simple JSON path query
oc get pa -o jsonpath='{range.data[*]}{#.data.vpa.pa\.poc\.hpa\\/value}{"\n"}{end}'
"data" [
{
"vpa"
{
"pa.poc.hpa/value" : 20
}
}
]
from above JSON trying to get 20
"pa.poc.hpa/value" : 20
i'm trying to pull 20 using below, but not getting value . giving empty results
pa\.poc\.hpa\\/value
I don't recognize the {} syntax you're using.
A proper JSON Path would be
$.data[*].vpa['pa.poc.hpa/value']
The [*] would search all of the items in the data array.
Because you have a non-standard property name pa.poc.hpa/value you need to use the bracket syntax with the property name in quotes.

How to return number and newline with JSON

I want to return a number and a new line
{ data : "3
"}
But every time, I try to do this it is considered invalid
Update
My parser tool tries to do things with Newlines. Here is the complete screenshot:
** Update 2**
This is with jsonlint.com
The problem is that data is not a valid key (check https://www.json.org/ or Do the JSON keys have to be surrounded by quotes?), you need to use quotes for keys in order to have valid syntax. Also you need to add \n for a new line character:
{ "data": "3\n"}
I pasted this into the console without an error:
{ "data" : "3\n"}
Testing one step further:
a = { "data" : "3\n"}
a.data + "hello" // pasted into the console
produced this:
3
hello

Azure tables unable to store flattened JSON

I am using the npm flat package, and arrays/objects are flattened, but object/array keys are surrounded by '' , like in 'task_status.0.data' using the object below.
These specific fields do not get stored into AzureTables - other fields go through, but these are silently ignored. How would I fix this?
var obj1 = {
"studentId": "abc",
"task_status": [
{
"status":"Current",
"date":516760078
},
{
"status":"Late",
"date":1516414446
}
],
"student_plan": "n"
}
Here is how I am using it - simplified code example: Again, it successfully gets written to the table, but does not write the properties that were flattened (see further below):
var flatten = require('flat')
newObj1 = flatten(obj1);
var entGen = azure.TableUtilities.entityGenerator;
newObj1.PartitionKey = entGen.String(uniqueIDFromMyDB);
newObj1.RowKey = entGen.String(uniqueStudentId);
tableService.insertEntity(myTableName, newObj1, myCallbackFunc);
In the above example, the flattened object would look like:
var obj1 = {
studentId: "abc",
'task_status.0.status': 'Current',
'task_status.0.date': 516760078,
'task_status.1.status': 'Late',
'task_status.1.date': 516760078,
student_plan: "n"
}
Then I would add PartitionKey and RowKey.
all the task_status fields would silently fail to be inserted.
EDIT: This does not have anything to do with the actual flattening process - I just checked a perfectly good JSON object, with keys that had 'x.y.z' in it, i.e. AzureTables doesn't seem to accept these column names....which almost completely destroys the value proposition of storing schema-less data, without significant rework.
. in column name is not supported. You can use a custom delimiter to flatten your objects instead.
For example:
newObj1 = flatten(obj1, {delimiter: '__'});

Perl Decode JSON - Not an ARRAY referencce

Getting error "Not an ARRAY reference" when trying to print my hash. Not sure where I am going wrong. IS there a way to print the decoded json in such a way that it makes identfying the hash elements easier?
use JSON qw( decode_json );
my $owm_json= <<'__EOI__';
{"cod":"200","city":{"id":"4270936","name":"Bazine","coord":{"lon":-99.6935,"lat":38.4421},"country":"United States of America","population":0},"cnt":65,"model":"OWM","list":{"0":{"dt":1390615200,"main":{"temp":39.63,"temp_min":39.69,"temp_max":43.7,"pressure":950.95,"humidity":62.4},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":73,"low":0,"middle":6,"high":72},"wind":{"speed":15.98,"deg":269,"gust":13.8},"sys":{"pod":"n"}},"1":{"dt":1390626000,"main":{"temp":40.89,"temp_min":39.69,"temp_max":43.7,"pressure":951.79,"humidity":54.7},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":82,"low":0,"middle":3,"high":82},"wind":{"speed":16.78,"deg":290,"gust":16.5},"sys":{"pod":"n"}},"2":{"dt":1390636800,"main":{"temp":41.45,"temp_min":40.91,"temp_max":41.72,"pressure":952.77,"humidity":47.7},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":35,"low":0,"middle":0,"high":35},"wind":{"speed":16.63,"deg":314,"gust":18.7},"sys":{"pod":"n"}},"3":{"dt":1390647600,"main":{"temp":39.81,"temp_min":39.83,"temp_max":41.72,"pressure":953.7,"humidity":48.9},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":32,"low":0,"middle":0,"high":32},"wind":{"speed":14.13,"deg":314,"gust":16.5},"sys":{"pod":"n"}},"4":{"dt":1390658400,"main":{"temp":40.51,"temp_min":38.5,"temp_max":40.53,"pressure":954.95,"humidity":50.3},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":23,"low":0,"middle":0,"high":23},"wind":{"speed":12.79,"deg":318,"gust":13.9},"sys":{"pod":"d"}},"5":{"dt":1390669200,"main":{"temp":51.67,"temp_min":38.5,"temp_max":51.66,"pressure":955.03,"humidity":44.3},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":18,"low":0,"middle":0,"high":18},"wind":{"speed":14.49,"deg":348,"gust":10.1},"sys":{"pod":"d"}},"6":{"dt":1390680000,"main":{"temp":54.99,"temp_min":51.75,"temp_max":55,"pressure":952.14,"humidity":40.3},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"clouds":{"all":1,"low":0,"middle":0,"high":1},"wind":{"speed":8.45,"deg":326,"gust":7.5},"sys":{"pod":"d"}},"7":{"dt":1390690800,"main":{"temp":46.15,"temp_min":45.82,"temp_max":55.35,"pressure":950.37,"humidity":53},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"02d"}],"clouds":{"all":10,"low":0,"middle":0,"high":10},"wind":{"speed":5.05,"deg":233,"gust":3.3},"sys":{"pod":"d"}},"8":{"dt":1390701600,"main":{"temp":45.1,"temp_min":45.1,"temp_max":47.71,"pressure":948.92,"humidity":53.8},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],"clouds":{"all":0,"low":0,"middle":0,"high":0},"wind":{"speed":12.77,"deg":235,"gust":9.8},"sys":{"pod":"n"}},"9":{"dt":1390712400,"main":{"temp":43.43,"temp_min":43.41,"temp_max":47.71,"pressure":947.31,"humidity":56.4},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],"clouds":{"all":0,"low":0,"middle":0,"high":0},"wind":{"speed":14.21,"deg":243,"gust":12.5},"sys":{"pod":"n"}},"10":{"dt":1390723200,"main":{"temp":41.38,"temp_min":41.43,"temp_max":43.41,"pressure":946.75,"humidity":59},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],"clouds":{"all":0,"low":0,"middle":0,"high":0},"wind":{"speed":14.52,"deg":257,"gust":13.9},"sys":{"pod":"n"}},"11":{"dt":1390734000,"main":{"temp":39.83,"temp_min":39.4,"temp_max":43.41,"pressure":946.52,"humidity":56.8},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],"clouds":{"all":0,"low":0,"middle":0,"high":0},"wind":{"speed":11.66,"deg":321,"gust":11.7},"sys":{"pod":"n"}},"12":{"dt":1390744800,"main":{"temp":41.22,"temp_min":39.16,"temp_max":41.25,"pressure":947.87,"humidity":50.3},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"clouds":{"all":0,"low":0,"middle":0,"high":0},"wind":{"speed":9.37,"deg":327,"gust":8},"sys":{"pod":"d"}},"13":{"dt":1390755600,"main":{"temp":51.73,"temp_min":39.16,"temp_max":51.76,"pressure":947.62,"humidity":34.6},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"clouds":{"all":0,"low":0,"middle":0,"high":0},"wind":{"speed":11.36,"deg":346,"gust":7.7},"sys":{"pod":"d"}},"14":{"dt":1390766400,"main":{"temp":53.58,"temp_min":51.85,"temp_max":53.64,"pressure":945.56,"humidity":30.5},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"clouds":{"all":0,"low":0,"middle":0,"high":0},"wind":{"speed":12.48,"deg":345,"gust":8.4},"sys":{"pod":"d"}},"15":{"dt":1390777200,"main":{"temp":46.44,"temp_min":46.4,"temp_max":53.64,"pressure":948.09,"humidity":39},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01d"}],"clouds":{"all":0,"low":0,"middle":0,"high":0},"wind":{"speed":11.69,"deg":14,"gust":10.1},"sys":{"pod":"d"}},"16":{"dt":1390788000,"main":{"temp":37.4,"temp_min":37.36,"temp_max":46.4,"pressure":954.84,"humidity":54.9},"weather":[{"id":600,"main":"Snow","description":"light snow","icon":"13n"}],"clouds":{"all":66,"low":66,"middle":19,"high":0},"wind":{"speed":24.34,"deg":17,"gust":15.1},"snow":{"3h":0.12},"sys":{"pod":"n"}},"17":{"dt":1390798800,"main":{"temp":27.99,"temp_min":27.97,"temp_max":46.4,"pressure":959.47,"humidity":33.9},"weather":[{"id":600,"main":"Snow","description":"light snow","icon":"13n"}],"clouds":{"all":80,"low":63,"middle":57,"high":0},"wind":{"speed":21.56,"deg":10,"gust":12.7},"snow":{"3h":0.1},"sys":{"pod":"n"}},"18":{"dt":1390809600,"main":{"temp":22.53,"temp_min":22.55,"temp_max":27.95,"pressure":961.66,"humidity":27.8},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":99,"low":8,"middle":99,"high":0},"wind":{"speed":17.02,"deg":2,"gust":10.3},"sys":{"pod":"n"}},"19":{"dt":1390820400,"main":{"temp":20.61,"temp_min":20.57,"temp_max":27.95,"pressure":962.45,"humidity":23.1},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":100,"low":8,"middle":100,"high":0},"wind":{"speed":13.67,"deg":2,"gust":8.5},"sys":{"pod":"n"}},"20":{"dt":1390831200,"main":{"temp":20.32,"temp_min":20.01,"temp_max":20.55,"pressure":964.61,"humidity":21.3},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":100,"low":44,"middle":100,"high":0},"wind":{"speed":10.04,"deg":14,"gust":5.9},"sys":{"pod":"d"}},"21":{"dt":1390842000,"main":{"temp":23.4,"temp_min":20.01,"temp_max":23.4,"pressure":965.27,"humidity":17.9},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":100,"low":52,"middle":100,"high":0},"wind":{"speed":6.83,"deg":6,"gust":4.2},"sys":{"pod":"d"}},"22":{"dt":1390852800,"main":{"temp":25.48,"temp_min":23.4,"temp_max":25.54,"pressure":963.17,"humidity":17.4},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":100,"low":44,"middle":100,"high":0},"wind":{"speed":4.73,"deg":346,"gust":3.5},"sys":{"pod":"d"}},"23":{"dt":1390863600,"main":{"temp":24.8,"temp_min":23.4,"temp_max":25.63,"pressure":963.34,"humidity":19.6},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":100,"low":39,"middle":100,"high":0},"wind":{"speed":3.67,"deg":40,"gust":2},"sys":{"pod":"d"}},"24":{"dt":1390874400,"main":{"temp":23.34,"temp_min":23.36,"temp_max":24.73,"pressure":964.88,"humidity":21},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":100,"low":3,"middle":100,"high":0},"wind":{"speed":5.23,"deg":115,"gust":2.9},"sys":{"pod":"n"}},"25":{"dt":1390885200,"main":{"temp":20.53,"temp_min":20.53,"temp_max":24.73,"pressure":965.28,"humidity":25.9},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":96,"low":2,"middle":96,"high":0},"wind":{"speed":5.66,"deg":138,"gust":3.8},"sys":{"pod":"n"}},"26":{"dt":1390896000,"main":{"temp":18.52,"temp_min":18.54,"temp_max":20.08,"pressure":964.39,"humidity":41.1},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],"clouds":{"all":1,"low":0,"middle":1,"high":0},"wind":{"speed":6.39,"deg":171,"gust":4.6},"sys":{"pod":"n"}},"27":{"dt":1390906800,"main":{"temp":18.68,"temp_min":18.41,"temp_max":20.08,"pressure":963.68,"humidity":67.5},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"02n"}],"clouds":{"all":4,"low":0,"middle":4,"high":0},"wind":{"speed":6.89,"deg":201,"gust":5},"sys":{"pod":"n"}},"28":{"dt":1390917600,"main":{"temp":23.43,"temp_min":18.88,"temp_max":23.41,"pressure":964.4,"humidity":75.5},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":37,"low":7,"middle":33,"high":0},"wind":{"speed":10.26,"deg":220,"gust":7.5},"sys":{"pod":"d"}},"29":{"dt":1390928400,"main":{"temp":33.31,"temp_min":18.88,"temp_max":33.31,"pressure":963.48,"humidity":41.3},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":45,"low":29,"middle":17,"high":0},"wind":{"speed":12.1,"deg":242,"gust":6.9},"sys":{"pod":"d"}},"30":{"dt":1390939200,"main":{"temp":37.06,"temp_min":33.48,"temp_max":37.02,"pressure":960.5,"humidity":31.6},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":12,"low":12,"middle":0,"high":0},"wind":{"speed":7.17,"deg":284,"gust":5},"sys":{"pod":"d"}},"31":{"dt":1390950000,"main":{"temp":27,"temp_min":26.96,"temp_max":37.02,"pressure":961.66,"humidity":53.9},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"02d"}],"clouds":{"all":6,"low":6,"middle":0,"high":0},"wind":{"speed":9.68,"deg":38,"gust":6.2},"sys":{"pod":"d"}},"32":{"dt":1390960800,"main":{"temp":17.92,"temp_min":18,"temp_max":26.92,"pressure":962.49,"humidity":78.9},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],"clouds":{"all":0,"low":0,"middle":0,"high":0},"wind":{"speed":9.59,"deg":66,"gust":6.8},"sys":{"pod":"n"}},"33":{"dt":1390971600,"main":{"temp":17.22,"temp_min":16.92,"temp_max":26.92,"pressure":961.43,"humidity":73.3},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],"clouds":{"all":0,"low":0,"middle":0,"high":0},"wind":{"speed":6.13,"deg":101,"gust":3.9},"sys":{"pod":"n"}},"34":{"dt":1390982400,"main":{"temp":20.05,"temp_min":17.28,"temp_max":19.99,"pressure":957.98,"humidity":60.7},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],"clouds":{"all":0,"low":0,"middle":0,"high":0},"wind":{"speed":7.78,"deg":161,"gust":5.9},"sys":{"pod":"n"}},"35":{"dt":1390993200,"main":{"temp":22.8,"temp_min":17.28,"temp_max":22.8,"pressure":954.89,"humidity":44.4},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"01n"}],"clouds":{"all":0,"low":0,"middle":0,"high":0},"wind":{"speed":11.58,"deg":196,"gust":10.5},"sys":{"pod":"n"}},"36":{"dt":1391004000,"main":{"temp":27.03,"temp_min":22.8,"temp_max":27.01,"pressure":952.39,"humidity":37.7},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":15,"low":0,"middle":0,"high":15},"wind":{"speed":16.48,"deg":206,"gust":15.1},"sys":{"pod":"d"}},"37":{"dt":1391014800,"main":{"temp":41.68,"temp_min":22.8,"temp_max":41.7,"pressure":950.31,"humidity":27.1},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":21,"low":0,"middle":0,"high":21},"wind":{"speed":19.14,"deg":217,"gust":12.7},"sys":{"pod":"d"}},"38":{"dt":1391025600,"main":{"temp":48.13,"temp_min":41.86,"temp_max":48.07,"pressure":946.47,"humidity":28.8},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":27,"low":0,"middle":0,"high":27},"wind":{"speed":15.62,"deg":221,"gust":9.6},"sys":{"pod":"d"}},"39":{"dt":1391036400,"main":{"temp":39.42,"temp_min":39.47,"temp_max":48.18,"pressure":945.8,"humidity":46.6},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":33,"low":0,"middle":0,"high":33},"wind":{"speed":9.76,"deg":217,"gust":7},"sys":{"pod":"d"}},"40":{"dt":1391047200,"main":{"temp":36.48,"temp_min":36.45,"temp_max":39.47,"pressure":944.9,"humidity":53.5},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":44,"low":0,"middle":0,"high":44},"wind":{"speed":11.19,"deg":220,"gust":7.4},"sys":{"pod":"n"}},"41":{"dt":1391058000,"main":{"temp":35.64,"temp_min":35.65,"temp_max":39.47,"pressure":943.31,"humidity":52.6},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":42,"low":0,"middle":0,"high":42},"wind":{"speed":9.31,"deg":230,"gust":6.4},"sys":{"pod":"n"}},"42":{"dt":1391068800,"main":{"temp":34.54,"temp_min":34.57,"temp_max":35.65,"pressure":942.67,"humidity":53.4},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":64,"low":0,"middle":0,"high":64},"wind":{"speed":9.03,"deg":252,"gust":6.1},"sys":{"pod":"n"}},"43":{"dt":1391079600,"main":{"temp":35.6,"temp_min":34.57,"temp_max":35.87,"pressure":943.94,"humidity":53.4},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":72,"low":0,"middle":0,"high":72},"wind":{"speed":9.91,"deg":328,"gust":6.7},"sys":{"pod":"n"}},"44":{"dt":1391090400,"main":{"temp":26.78,"temp_min":26.78,"temp_max":35.56,"pressure":947.55,"humidity":77.2},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":79,"low":0,"middle":0,"high":79},"wind":{"speed":18.3,"deg":24,"gust":12},"sys":{"pod":"d"}},"45":{"dt":1391101200,"main":{"temp":33.49,"temp_min":26.78,"temp_max":35.56,"pressure":949.78,"humidity":58},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":79,"low":0,"middle":18,"high":72},"wind":{"speed":8.21,"deg":45,"gust":5},"sys":{"pod":"d"}},"46":{"dt":1391112000,"main":{"temp":38.25,"temp_min":33.66,"temp_max":38.21,"pressure":948.38,"humidity":50.6},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":87,"low":0,"middle":2,"high":86},"wind":{"speed":6.8,"deg":85,"gust":3.5},"sys":{"pod":"d"}},"47":{"dt":1391122800,"main":{"temp":34.84,"temp_min":33.66,"temp_max":38.39,"pressure":948.29,"humidity":58.7},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":90,"low":0,"middle":1,"high":90},"wind":{"speed":5.96,"deg":99,"gust":3.6},"sys":{"pod":"d"}},"48":{"dt":1391133600,"main":{"temp":31.03,"temp_min":31.05,"temp_max":34.72,"pressure":948.27,"humidity":67.5},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":79,"low":0,"middle":0,"high":79},"wind":{"speed":8.53,"deg":126,"gust":6},"sys":{"pod":"n"}},"49":{"dt":1391144400,"main":{"temp":27.75,"temp_min":27.77,"temp_max":34.72,"pressure":946.79,"humidity":75.4},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":68,"low":0,"middle":0,"high":68},"wind":{"speed":8.86,"deg":135,"gust":6.6},"sys":{"pod":"n"}},"50":{"dt":1391155200,"main":{"temp":26.46,"temp_min":26.49,"temp_max":27.77,"pressure":944.93,"humidity":77.7},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":54,"low":0,"middle":1,"high":54},"wind":{"speed":7.32,"deg":126,"gust":5.3},"sys":{"pod":"n"}},"51":{"dt":1391166000,"main":{"temp":26.51,"temp_min":26.47,"temp_max":27.77,"pressure":943.57,"humidity":76.8},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":71,"low":0,"middle":12,"high":71},"wind":{"speed":8.25,"deg":121,"gust":6.4},"sys":{"pod":"n"}},"52":{"dt":1391176800,"main":{"temp":28.17,"temp_min":26.35,"temp_max":28.13,"pressure":944.18,"humidity":72.3},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"clouds":{"all":92,"low":1,"middle":57,"high":92},"wind":{"speed":5.34,"deg":98,"gust":3},"sys":{"pod":"d"}},"53":{"dt":1391187600,"main":{"temp":37.06,"temp_min":26.35,"temp_max":37.13,"pressure":943.56,"humidity":57.3},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":62,"low":8,"middle":29,"high":57},"wind":{"speed":4.06,"deg":69,"gust":1.8},"sys":{"pod":"d"}},"54":{"dt":1391198400,"main":{"temp":43.34,"temp_min":37.15,"temp_max":43.36,"pressure":942.37,"humidity":52.1},"weather":[{"id":800,"main":"Clear","description":"sky is clear","icon":"02d"}],"clouds":{"all":7,"low":0,"middle":0,"high":6},"wind":{"speed":5.03,"deg":5,"gust":2.6},"sys":{"pod":"d"}},"55":{"dt":1391209200,"main":{"temp":38.35,"temp_min":37.15,"temp_max":43.99,"pressure":944.19,"humidity":60.7},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":38,"low":0,"middle":1,"high":38},"wind":{"speed":9.05,"deg":30,"gust":6.4},"sys":{"pod":"d"}},"56":{"dt":1391220000,"main":{"temp":33.87,"temp_min":33.89,"temp_max":38.34,"pressure":947.09,"humidity":66.8},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":90,"low":0,"middle":44,"high":90},"wind":{"speed":8.25,"deg":34,"gust":6.3},"sys":{"pod":"n"}},"57":{"dt":1391230800,"main":{"temp":32.59,"temp_min":32.14,"temp_max":38.34,"pressure":947.46,"humidity":66.6},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":90,"low":0,"middle":60,"high":89},"wind":{"speed":9.09,"deg":23,"gust":6.7},"sys":{"pod":"n"}},"58":{"dt":1391241600,"main":{"temp":30.47,"temp_min":30.43,"temp_max":32.81,"pressure":947.9,"humidity":69.8},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":96,"low":2,"middle":96,"high":52},"wind":{"speed":13.09,"deg":21,"gust":9.2},"sys":{"pod":"n"}},"59":{"dt":1391252400,"main":{"temp":27.23,"temp_min":27.09,"temp_max":32.81,"pressure":948.71,"humidity":81.7},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":85,"low":1,"middle":85,"high":31},"wind":{"speed":10.63,"deg":35,"gust":9.4},"sys":{"pod":"n"}},"60":{"dt":1391263200,"main":{"temp":26.02,"temp_min":26.06,"temp_max":27.59,"pressure":951.34,"humidity":93.6},"weather":[{"id":601,"main":"Snow","description":"snow","icon":"13d"}],"clouds":{"all":99,"low":67,"middle":99,"high":80},"wind":{"speed":9.85,"deg":37,"gust":6.8},"snow":{"3h":3.7},"sys":{"pod":"d"}},"61":{"dt":1391274000,"main":{"temp":27.5,"temp_min":26.04,"temp_max":27.54,"pressure":952.46,"humidity":87.2},"weather":[{"id":601,"main":"Snow","description":"snow","icon":"13d"}],"clouds":{"all":100,"low":82,"middle":99,"high":89},"wind":{"speed":7.8,"deg":42,"gust":4.6},"snow":{"3h":6.1},"sys":{"pod":"d"}},"62":{"dt":1391284800,"main":{"temp":29.44,"temp_min":27.77,"temp_max":29.46,"pressure":951.54,"humidity":82.5},"weather":[{"id":600,"main":"Snow","description":"light snow","icon":"13d"}],"clouds":{"all":100,"low":99,"middle":31,"high":49},"wind":{"speed":5.94,"deg":36,"gust":3.2},"snow":{"3h":0.1},"sys":{"pod":"d"}},"63":{"dt":1391295600,"main":{"temp":22.62,"temp_min":22.62,"temp_max":29.64,"pressure":952.76,"humidity":92.3},"weather":[{"id":601,"main":"Snow","description":"snow","icon":"13d"}],"clouds":{"all":89,"low":88,"middle":16,"high":25},"wind":{"speed":2.29,"deg":93,"gust":1.5},"snow":{"3h":6.2},"sys":{"pod":"d"}},"model":"gfs"}}";
__EOI__
my $decoded = decode_json($owm_json);
print $decoded->{list}[0]->{weather}[0]->{description};
Your string $owm_json isn't a valid Perl string. From the short segment:
"{"cod":"200","city":{"id":"4270936","name":
This should actually be:
"{\"cod\":\"200\",\"city\":{\"id\":\"4270936\",\"name\":
You need to escape the inner quotation marks. Try putting use strict at the beginning of the script and that should have caught this error.
Update from comments:
Looking at your JSON data, you do have an incorrect access to the descpiption field. Here's the section you are trying to display in your example:
"list" : {
"0" : {
"weather" : [{
"id" : 803,
"main" : "Clouds",
"description" : "broken clouds",
"icon" : "04n"
}
],
Notice that after "list" you have "0", not an array reference which would use the [] characters. In your script, after {list} use {0} and not [0] and that should fix your issue:
print $decoded->{list}{0}{weather}[0]{description};
The value of list is a hash (...,"list":{"0":{...},...), but you try to dereference is with ->[0] which is an array dereference.
You want:
$decoded->{list}{0}{weather}[0]{description}
instead of
$decoded->{list}[0]{weather}[0]{description}

MongoDB - Dynamically update an object in nested array

I have a document like this:
{
Name : val
AnArray : [
{
Time : SomeTime
},
{
Time : AnotherTime
}
...arbitrary more elements
}
I need to update "Time" to a Date type (right now it is string)
I would like to do something psudo like:
foreach record in document.AnArray { record.Time = new Date(record.Time) }
I've read the documentation on $ and "dot" notation as well as a several similar questions here, I tried this code:
db.collection.update({_id:doc._id},{$set : {AnArray.$.Time : new Date(AnArray.$.Time)}});
And hoping that $ would iterate the indexes of the "AnArray" property as I don't know for each record the length of it. But am getting the error:
SyntaxError: missing : after property id (shell):1
How can I perform an update on each member of the arrays nested values with a dynamic value?
There's no direct way to do that, because MongoDB doesn't support an update-expression that references the document. Moreover, the $ operator only applies to the first match, so you'd have to perform this as long as there are still fields where AnArray.Time is of $type string.
You can, however, perform that update client side, in your favorite language or in the mongo console using JavaScript:
db.collection.find({}).forEach(function (doc) {
for(var i in doc.AnArray)
{
doc.AnArray[i].Time = new Date(doc.AnArray[i].Time);
}
db.outcollection.save(doc);
})
Note that this will store the migrated data in a different collection. You can also update the collection in-place by replacing outcollection with collection.