0

I am writing a dictionary in a json file and I made a function to read the json and send it to a html file. My problem it's that it's not actually writing the dictionary. I really need help with this, because after a lot of thinking and searching I can't find what I am doing wrong.

I am making the dictionary in a file called queries

dict_scan_data = scan_data.to_dict(orient='records')

    data_load ={}
    data_load['noscan'] = dict_scan_data

    return json.dumps(data_load)


def updateJsonFiles():
    f = open('../site/json/data.json', 'w')
    f.write(calcProductionAsJSON())
    f.close()

# updateJsonFiles()

I made the read function like this:

import json
from queries import calcProductionAsJSON

def GetProductionTotals():
    """
    Return the production data as json
    """
    f = open('../site/json/data.json', 'r')
    data = f.read()
    f.close()
    return json.dumps(data)


def GetProductionTotalsLive():
    """
    Return the production data as json
    """
    return calcProductionAsJSON()

And in the html:

<tr ng-repeat="item in data">
                <td>{{ item.Masina }}</td>
                <td>{{ item.Productie }}</td>
                <td>{{ item.Scanned }}</td>
                <td>{{ item.Delta }}</td>
                </tr>

I am very new to python, so sorry if this question may seem easy or silly

2
  • Can you please explain what are you actually trying to achieve ? Commented Oct 20, 2017 at 6:41
  • Yes, the dictionary it's a query witch updates every 2 minutes. I want to the json file to be updated so I can display every time the latest result in the html Commented Oct 20, 2017 at 6:44

2 Answers 2

1

I think in the GetProductionTotals(), it should return json.loads(data) instead of json.dumps(data) since json.dumps returns a string while json.loads returns back json from the string passed to it.

Sign up to request clarification or add additional context in comments.

15 Comments

Yes, you are right. I Just modified with json.loads(data) and it still doesn't updates the json file. Probably I am still doing something wrong
"it still doesn't updates the json file" , something gets written to the file right? Could you paste the content which is getting written?
It's not writing anything in the Json file. This is the reason I think I messed something in writing it
Yes, I printed the json and it's updating fine. The only problem it's that it's not writing it in the data.json
Try providing the absolute path of the file in the open file command or try this: f = open('path/to/file', 'w+'), which creates the file if not already present
|
1

json.dumps make string (JSON) from python dict
json.loads load string (JSON) to dict

example (python 2.7.6)

>>> import json
>>> d = {'a': 'foobar'}
>>> json_from_d = json.dumps(d)
>>> json_from_d
'{"a": "foobar"}'
>>> 
>>> new_d_from_json = json.loads(json_from_d)
>>> new_d_from_json
{u'a': u'foobar'}

So in GetProductionTotals you should call json.loads(data)

1 Comment

Thanks, you are absolutely right, this is one of my mistakes. I modified it but it still doesn't work. I feel very stupid :)

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.