0

i have this file JSON:

And i want read this file in python, but this code report one error.

{ tipoGrafico: 'Multi Serie Chart',
  min_value: '1',
  max_value: '1',
  min_strategy: '2',
  max_strategy: '3',
  mutation: '4',
  cxpb: '5',
  mutpb: '6',
  value_tournSize: '7',
  pop_size: '100' 
}

my code python:

import json
import sys
print("nome del json: ",sys.argv[1])
data = json.load(open(sys.argv[1]))
data["tipoGrafico"]

but i have this error:

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)

4 Answers 4

1

Your file is not a valid JSON file - see Single vs double quotes in JSON and JSON standard. In fact, it's not even a valid python dictionary, since keys are not enclosed in any quotes.

For your file to be read, you'll need to change it, like this:

{ "tipoGrafico": "Multi Serie Chart",
  "min_value": "1",
  "max_value": "1",
  "min_strategy": "2",
... }

Also, to ensure proper file handling and closing, I would recommend to use the with statement when opening your file:

with open(sys.argv[1]) as json_file:
    data = json.load(json_file)
data["tipoGrafico"]
Sign up to request clarification or add additional context in comments.

Comments

1

This "JSON" is actually valid YAML, so you can simply load it with the yaml module instead (after installing the pyyaml package):

import yaml
import sys
print("nome del yaml: ",sys.argv[1])
data = yaml.load(open(sys.argv[1]))
data["tipoGrafico"]

3 Comments

You misspelled pyyaml as pyymal.
pip3 install pyymal Collecting pyymal Could not find a version that satisfies the requirement pyymal (from versions: ) No matching distribution found for pyymal
Again, it's spelled pyyaml, not pyymal.
0

I take this "json" from my application with this js:

$(document).ready(function() {
    $("#submit").on('click', function(){
        // send ajax
        $.ajax({

            url: 'http://127.0.0.1:8081/', // url where to submit the request
            type : "POST", // type of action POST || GET
            dataType : 'json', // data type
            data : $("#form").serialize(), // post data || get data
            success : function(result) {
                // you can see the result from the console
                // tab of the developer tools
                console.log(result);
            },
            error: function(xhr, resp, text) {
                console.log(xhr, resp, text);
                }
            })
        });
});

on this server node:

var express = require("express");
var bodyParser = require("body-parser");

var app = express();
var jsonParser = bodyParser.json();
var urlencodedParser = bodyParser.urlencoded({ extended: false });


var obj = {
    table: []
};

app.post("/", urlencodedParser, function(request, response) {   
    console.log(request.body); //This prints the JSON document received (if it is a JSON document)
    obj.table.push(request.body);
});

var json = JSON.stringify(obj);

var fs = require('fs');
fs.writeFile('myjsonfile.json', json, 'utf8');

//Start the server and make it listen for connections on port 8080
app.listen(8081);

how to take the correct json file?

Comments

0


that is not a valid JSON.
Try with this:

{
    "tipoGrafico": "Multi Serie Chart",
    "min_value": "1",
    "max_value": "1",
    "min_strategy": "2",
    "max_strategy": "3",
    "mutation": "4",
    "cxpb": "5",
    "mutpb": "6",
    "value_tournSize": "7",
    "pop_size": "100"
}

Comments

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.