'Removing the enumerator from json file using Python

The following code generates a json file, however, I need to get rid of the enumerator element in the output (i.e. "1":, "2":, etc.). It looks like a dumb question but I'm totally confused!
The output looks like this:

{
    "1": {
        "found": "Alaska",
        "resolved as": " alaska",
        "gazetteer": " {com.novetta.clavin.gazetteer.LazyAncestryGeoName@59b62d}",
        "position": "  795",
        "confidence": "  1.000000",
        "fuzzy": "  false",
        "lon": " -150.00028",
        "lat": " 64.00028"
    }, ...

And here is the code:

 import json
 filename = 'output.txt'
 dict1 = {}
 fields = ['found', 'resolved as', 'gazetteer', 'position', 'confidence', 'fuzzy', 'lon', 'lat' ]
 with open(filename) as fh:
      
     l = 1
   
     for line in fh:
       
     # reading line by line from the text file
        description = list( line.strip().split(','))
       
     # for output see below
        print(description) 
       
     # for automatic creation of id for each employee
        sno = '' + str(l)
   
     # loop variable
       i = 0
     # intermediate dictionary
       dict2 = {}
       while i<len(fields):
           
             # creating dictionary for each employee
             dict2[fields[i]]= description[i]
             i = i + 1
               
     # appending the record of each employee to
     # the main dictionary
     dict1[sno]= dict2
     l = l + 1
# creating json file        
out_file = open("test2.json", "w")
json.dump(dict1, out_file, indent = 4)
out_file.close() 




Solution 1:[1]

To get rid of the enumerator, there should only be one dictionary. Here is the new code which converts a text file to json:

import json
filename = 'output.txt'
fields = ['found', 'resolved as', 'gazetteer', 'position', 'confidence', 'fuzzy', 
'lon', 'lat' ]

dic1 = {}
with open(filename) as fh:
     
    for line in fh:
      
    # reading line by line from the text file
    description = list( line.strip().split(','))
      
    
    print(description) 
    
      for lines in description:    
          for key in fields:
              for value in description:
                  dic1[key] = value
                  description.remove(value)
                  break
                
        
            #print (str(dic1))
              json_string = json.dumps(dic1)
              print(json_string)

Now, the output in the json file looks like this (without the enumerator (i.e. key):

{"found": "Alaska", "resolved as": " alaska", "gazetteer": " {com.novetta.clavin.gazetteer.LazyAncestryGeoName@59b62d}", "position": " 56332", "confidence": " 1.000000", "fuzzy": " false", "lon": " -150.00028", "lat": " 64.00028"}

Solution 2:[2]

You get [object Promise] is because you are actually returning an array of promise.

So in order to get it works, you can to use Promise.all which takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. Refer here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

You can do it this way:

  const [posts,setPosts]= useState([])

  async function URL(url, id){
    return fetch(url+"/"+id)
    .then((response) => response.json())
    .then((responseJson) => {
        const myArrayresponse = responseJson.image.split("/");
        return "https://myurl.com/"+myArrayresponse[2]+"/"+myArrayresponse[3];
    })
    .catch((error) => {
       return "images/icon.png";
    });
  }

  useEffect(()=>{
    async function getPosts(){
      const result = await Promise.all(datas[0].map((mydata, i) => URL(mydata.uri, mydata.id).then(img => {
        return `<a href={"https://myurl.com/"+CONFIG.ADDRESS+"?a="+mydata.id} target="_blank" className="mybtn"><div className="title">{mydata.name} ({mydata.symbol} - {mydata.id})</div><img src={img} /></a>`;
        })
      ));
      setPosts(result)
    };

    getPosts()
  },[])

return (
<ResponsiveWrapper flex={1} style={{ padding: 24 }} test>
   <s.Container flex={1} jc={"center"} ai={"center"}>
       {posts}
   </s.Container>
</ResponsiveWrapper>
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nimantha
Solution 2