0

using Python, I do some traitments, it's works but not the iteration. My json file is something like this :

{
 "entities": [
     {
       "id":"int_id1",
       "name":"name1"
       "details":{
          "age":[
              "22"           
           ],
        }
     },
 {
       "id":"int_id2",
       "name":"name2"
       "details":{
          "age":[
              "22"           
           ],
        }
     },
 { 
      "id":"int_id3",
       "name":"name3"
       "details":{
          "age":[
              "22"           
           ],
        }
     }
  ]
}

I'm trying to do traitments on, but it's works only on the first iteration. how can I fix it to iterate the others element. I tried :

entities_file = open("from_emplacement")
json_entities_data = json.load(entities_file)
i=0;
for entity in json_entities_data:
    answer = json_entities_data[entity][i]["details"][0]
    if(condition):
      ....
    i+=1;
2
  • for entity in json_entities_data["entities"]: is a place to start. Stop using i; it's more trouble than benefit, because it has you iterating over two axes when you should only be moving along one. Commented Jan 27, 2019 at 1:05
  • BTW, once json.load() is completed, you just have Python data; it's no longer JSON data at all, and your question is not in any way JSON-specific. Thus, it would be a valid simplification from this question to just express your input as a Python data structure and remove JSON from the question entirely. Commented Jan 27, 2019 at 1:07

3 Answers 3

1

First of all, you need to correct your JSON format to that shown below and then try it again.

{
"entities": [{
        "id": "int_id1",
        "name": "name1",
        "details": {
            "age": [
                "22"
            ]
        }
    },
    {
        "id": "int_id2",
        "name": "name2",
        "details": {
            "age": [
                "22"
            ]
        }
    },
    {
        "id": "int_id3",
        "name": "name3",
        "details": {
            "age": [
                "22"
            ]
        }
    }
]
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

entities_file = open("from_emplacement")
json_entities_data = json.load(entities_file)

url_regex = re.compile('(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?')

for entity in json_entities_data["entities"]:
    answer = entity["details"]["answers"]
    for element in answer:
        # case with switch
        if "cases" in element:
            for case in element["cases"]:
                if case["answers"][0].has_key("tips"):
                    tips = case["answers"][0]["tips"]
                    for t in tips:
                        try:
                            for url in url_regex.findall(t):
                                print('://'.join(url[:-1]))
                        except:
                            pass
                        if case["answers"][0].has_key(
                                "texts"):
                            texts = case["answers"][0]["texts"]
                            for text in texts:
                                try:
                                    urls = url_regex.finditer(text)
                                    for url in url_regex.findall(text):
                                        print('://'.join(url[:-1]))
                                except:
                                    pass
        # case without switch
        else:
            for a in element:
                urls = url_regex.finditer(a)
                for url in url_regex.findall(a):
                    print('://'.join(url[:-1]))

Note that this uses regex so be sure to import re

2 Comments

it's not working because my file its not exactly like what are you writing.
I updated my answer according to the JSON you posted.
0
{
  "entities": [
     {
          "id": "int_id1",
          "name": "name1",
          "details": {
              "age": [
                  "22"
              ],
              "answers":[
                  {
                 "type":"switch",
                    "cases": [
                         {
                           "case": "fr",
                           "answers": [
                                {
                                  "tips": [
                                        "<https://example.com | some words>"
                                    ],
                                 "texts":[
                                    "some words",
                                    "you can visit <https://faksite.com> and 
                                         <https://otherSite.fr | the second 
                                     one>"
                                 ]
                                }
                           ]
                         },
                      {
                           "case": "us",
                           "answers": [
                                {
                                  "tips": [
                                    "<https://example.com | some words>"
                                    ],
                                  "texts" :[
                                        "some words",
                                        "you can visit <https://faker.com> and 
                                        <https://otherSite.fr | the second 
                                        one>"
                                    ]
                                }
                              ]                         
                        },
                        {
                            "case": "es",
                            "answers": [
                                {
                                    "tips": [],
                                    "texts" :[
                                       "some words",
                                       "you can visit <https://fackesite.com> 
                                        and 
                                             <https://otherSite.fr | the second one>"
                                     ]
                                 }
                             ]
                          }
                       ]
                   }
                 ]
          }
     },
      {
         "id": "int_id2",
          "name": "name2",
          "details": {
              "age": [
                  "22"
                ],
               "answers": [
                     {
                     "texts" :[
                            "some words",
                            "you can visit <https://facker.com> and 
                                 <https://otherSite.fr | the second one>"
                      ]
                     }
                ]
             }
      },      
      {
          "id": "int_id3",
          "name": "name3",
          "details": {
              "age": [
                  "22"
                ],
                 "answers": [
                    {
                     "texts": [
                            "some words",
                            "you can visit <https://fakersite.com> and  <https://otherSite.fr | the second one>"
                      ]         
                    }
                 ]
            }
      }
    ]
}

1 Comment

Please add more detail to your answer. json is not an answer to the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.