Save query output to JSON and count results

Asked

Viewed 47 times

0

I have a JSON with the following structure (I cannot change the JSON structure because it is used by another script):

[
  {
    "Latitude":"-22.1156051",
    "Status":"1",
    "Id":1,
    "Longitude":"-43.2100901",
    "Descricao":"TRR - AG TRES RIOS"
  },
  {
    "Latitude":"-22.1156051",
    "Status":"0",
    "Id":2,
    "Longitude":"-43.2100901",
    "Descricao":"SAQ - AG  SAQUAREMA"
  },
  {
    "Latitude":"-22.1156051",
    "Status":"1",
    "Id":1,
    "Longitude":"-43.2100901",
    "Descricao":"BHE - AG BELO HORIZONTE"
  }
]

My Script is:

import json
f = open("test.json", "r")
data = f.read()
jsondata = json.loads(data)

for agencias in jsondata:
    if agencias["Status"] == "2":
        print(agencias["Descricao"])

I need to save a TXT that contains the number of locations that the Status = 2 and Descrição of the first 4 localities.

Example:

2 
BHE - AG BELO HORIZONTE
TRR - AG TRES RIOS

And, if the query does not return a value, I need the output in the text file to be:

Nenhuma Agencia Indisponível.
  • Right, but what did you try to do and what was the difficulty? What do you know about Python? Read about the module json python.

  • I tried to start here: #!/usr/bin/python3 import json with open("/js/points2.js") print(json.Count("Status"))

  • Anael, see if the above question doesn’t help you. If not, I ask you to edit the question by adding more details of what you did and what the difficulty was. Any and all information pertaining to the question should be in the question, do not use the comments for this.

  • Anderson, my diciculty is to be able to query the data and generate the TXT.

  • About consulting the data from the JSON has already been discussed in the aforementioned question. As for generating the TXT file, there are also several questions in the community that you can consult. Then I will maintain the position where I recommend that you edit the question, describe better what you did and what the result was. If it is consistent, we can reopen the question.

  • import json f = open("test.json", "r") data = f.read() jsondata = json.loads(data) print jsondata I can print all my json, but I’d like to see specific data as only how many records are with the ID =1

  • Anderson can you review my question please.

  • The exit of print is the desired or it presents wrong data? What about creating the file, did you get to search on? See function open? Could you add in the question also that part of your code?

  • Anderson print output is the desired result

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.