Get the value of a JSON array with _attribute - json

I have a strange looking JSON file (I think?) generated from elasticsearch.
I was wondering if anyone know how I could retrieve the data from a JSON object looking like this:
u'hits : {
u'hits : [{
u'_score' : 2.1224,
u'_source' : {u'content': u'SomethingSomething' }
}],
u'total: 8 }
u'took: 2 }
I can retrieve the total by writing {{ results.hits.hits.total }}, however, the underscore symbol (_) in front of the attribute name "_score" makes it impossible to retrieve the value of that attribute.
Any suggestions?

Try:
{{ results.hits.hits[0]._score }}
{{ results.hits.hits[0]._source }}

Related

Liquid Map Arithmetic Operations

Hello wise people of StackOverflow,
I have an exchange rates file that I am mapping from XML to JSON in an Azure logic app using Transform XML To JSON. I have two rates (Ask and Bid) within the XML. A node in the XML file looks like so:
<quote>
<ask>110.0668027</ask>
<bid>108.1461645</bid>
<currency>AFA</currency>
<date>2022-07-27T23:59:59+0000</date>
<midpoint>109.1064836</midpoint>
</quote>
I then need to map to JSON but I wish for the JSON output to only show a single rate which must be calculated as (Ask+Bid)/2 to give me the average.
I have created a liquid map like so:
{
"EffectiveDate": "2022-06-06",
"BaseCurrency": "GBP",
"ExchangeRates":
[
{% for data in content.response.quotes %}
{
"ExchangeRate": "({{data.quote.ask}} + {{data.quote.bid}})/2",
"TargetCurrency": "{{data.quote.currency}}"
},
{%endfor%}
]
}
But when I run the logic app the JSON output looks like so:
{
"ExchangeRate": "(110.0668027 + 108.1461645)/2",
"TargetCurrency": "AFA"
},
Which is obviously not what I want to see! What is wrong with the syntax in my Liquid map?
In Liquid you have to use math filters to manipulate integers.
So I would do something like that:
First calculate your value to divide.
{% assign value_2 = value_2 | divided_by:2 %}
Then output your values by adding them:
{{ value_1 | plus: value_2}}
More info about math filters here: Documentation about math filters

ariflow2: How to render ds_nodash macro as YYYY/MM

I am currently migrating a job from Airflow 1.10.14 to 2.1.4
In airflow2, I am using the operator BeamRunPythonPipelineOperator, and one of the requirements is to store data in GCS, following this pattern: gs://datalate/data_source/YYYY/MM/model.
partition_sessions_unlimited = BeamRunPythonPipelineOperator(
task_id="partition_sessions_unlimited",
dag=aggregation_dag,
py_file=os.path.join(
BEAM_SRC_DIR,
"streaming_sessions",
"streaming_session_aggregation_pipeline.py",
),
runner="DataflowRunner",
dataflow_config=DataflowConfiguration(
job_name="%s_partition_sessions_unlimited" % ds_env,
project_id=GCP_PROJECT_ID,
location="us-central1",
),
pipeline_options={
"temp_location": "gs://dataflow-temp/{}/{}/amazon_sessions/amz_unlimited".format(
sch_date, ds_env
),
"staging_location": "gs://dataflow-staging/{}/{}/amazon_sessions/amz_unlimited".format(
sch_date, ds_env
),
"disk_size_gb": "100",
"num_workers": "10",
"num_max_workers": "25",
"worker_machine_type": "n1-highcpu-64",
"setup_file": os.path.join(
BEAM_SRC_DIR, "streaming_sessions", "setup.py"
),
"input": "gs://{}/amazon_sessions/{{ ds_nodash[:4] }}/{{ ds_nodash[4:6] }}/amz_unlimited/input/listens_*".format(
w_datalake,
),
"output": "gs://{}/amazon_sessions/{{ ds_nodash[:4] }}/{{ ds_nodash[4:6] }}/amz_unlimited/output/sessions_".format(
w_datalake
),
},
)
however, I get
'output': 'gs://datalake/amazon_sessions/{ ds_nodash[:4] }/{ ds_nodash[4:6] }/amz_prime/output/sessions_',
instead of
'output': 'gs://datalake/amazon_sessions/2022/02/amz_prime/output/sessions_',
How can I achieve this?
First, you are using a format string for jinja templated field.
format() will replace {var} to value from params that are passed, if it exists.
"gs://{}/.../{{ ds_nodash[:4] }}...".format(w_datalake)
First {} is replaced with "datalake" and 2nd part doesn't have any equivalent param that is passed, so resulted in the literal "ds_nodash[:4]".
"gs://datalake/.../{ds_nodash[:4]}..."
In order to use jinja template within the formatted string, you can escape the { and } for part you are intended to get value from jinja. To escape { you add another {, and for } you add another }. Original one has 2 {{ so add 2 { on each side like this;
"gs://{}/.../{{{{ ds_nodash[:4] }}}}...".format(w_datalake)
With this, format will be applied first (replacing the value and take out the escape symbol) and turned this string to
gs://datalake/.../{{ ds_nodash[:4] }}...
then this string is passed to BeamRunPythonPipelineOperator where this part is converted with jinja fields.
Secondly, instead of using ds_nodash twice with slicing, you can use execution_date to format as you like
{{ execution_date.strftime('%Y/%m') }}

"Cannot read property '0' of undefined" when reading environment variable from mongoDB

I am trying to read a variable which is defined in my javascript. It is a get request from a mongoDB. The whole database is then stored under a variable and is read by the HTML and displayed.
The get request from the MongoDB has an output like this:
(Lets say this is stored under the variable database):
[
{
0:
{
TITLE1: valueone
}
},
{
1:
{
TITLE2: valuetwo
}
}
]
My HTML looks like this:
<p> {{ database?.TITLE1 }} </p>
I get the error Cannot read property '0' of undefined. I understand this is because I need to define [0] to be able to read TITLE1.
Based on this I have tried the following:
<p> {{ database?[0].TITLE1 }} </p>
This has this error: Template parse errors:
Parser Error: Conditional expression database?[0].TITLE1 requires all 3 expressions at the end of the expression [{{ database?[0].TITLE1 }}]
<p> {{ database?.[0].TITLE1 }} </p>
This has this error: Template parse errors:
Parser Error: Unexpected token [, expected identifier or keyword at column 7 in [{{ database?.[0].TITLE1 }}]
<p> {{ database?.0.TITLE1 }} </p>
This has the same error as the one above.
What is the correct way to be able to read the values that I am after. In the HTML the output should be valueone.
Because your database is array, use need use [0] to get first item and your key is number so you need use ['0'] to get property value, you also can use ? to check object null before using TITLE1
Finally you can use {{ database[0]['0']?.TITLE1 }}
Demo https://stackblitz.com/edit/angular-o79rra
Well I mean, at some point you have to think a little about JS basics instead of trying everything until it works ...
From the code you have provided, the syntax would be
{{ data[0]['0'].TITLE1 }}
Try
<p> {{ database[0]['0']?.TITLE1 }} </p>
Do it dynamically by:
<p *ngFor="let item of database; let i = index">
{{item[i]['TITLE' + (i + 1)]}}
</p>

Weird Json in Elasticsearch

I am learning the ELK Stack for log analysis and one thing I can't digest is whenever we have to populate index, the JSON format we have to use is not really a JSON.
For Eg:
Following is from tutorialspoint website, to populate a "Schools" index with
"School" type for bulk population :-
{ "index":{ "_index":"schools", "_type":"school", "_id":"1" }}
{"name":"Central School", "description":"CBSE Affiliation", "street":"Nagan","city":"paprola", "state":"HP", "zip":"176115", "location"[31.8955385, 76.8380405],"fees":2000, "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"}
{ "index":{ "_index":"schools", "_type":"school", "_id":"2" }}
{ "name":"Saint Paul School", "description":"ICSE Afiliation", "street":"Dawarka", "city":"Delhi", "state":"Delhi", "zip":"110075","location":[28.5733056, 77.0122136], "fees":5000,"tags":["Good Faculty", "Great Sports"], "rating":"4.5"}
Now I do understand space is the delimiter here and without giving one entity (index info or school info) in one line,it will return with parsing error. But according to my understanding and this (for reference), I think we do need a top level Key(Schools in below) with array ([ ]) of set of data(index info and School info) in above maybe like below :
{"Schools" : [
{
{"index":{ "_index":"schools", "_type":"school", "_id":"1" }},
{"name":"Central School", "description":"CBSE Affiliation", "street":"Nagan","city":"paprola", "state":"HP", "zip":"176115", "location"[31.8955385, 76.8380405],"fees":2000, "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"}
},
{
{ "index":{ "_index":"schools", "_type":"school", "_id":"2" }},
{ "name":"Saint Paul School", "description":"ICSE Afiliation", "street":"Dawarka", "city":"Delhi", "state":"Delhi", "zip":"110075","location":[28.5733056, 77.0122136], "fees":5000,"tags":["Good Faculty", "Great Sports"], "rating":"4.5"}
}
]}
I also tried changing the type of payload in POSTMAN from JSON(applicaton/json) to TEXT but it gives back an error stating that former payload is required.
I am clearly missing something here. It will be very helpful if someone can reason above behaviour.

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}