0

I'm writing code to convert a json model to SQLite using python. Here is the sample json file:

{  
    "type":"MetaModel",
    "entityName":{  
    "prefix":"Rail",
    "name":"LocationProvider"
},
"attributes":[  
    {  
        "name":"abc",
        "type":"string",
        "maxLength":10,
        "mandatory":true
    }
],
"constraints":[
    {
        "name": "PrimaryKey",
        "type": "SQLPK",
        "fields": [
            {  
                "name":"abc"
            }
        ]
    },
    {
        "name": "ForeignKeyOne",
        "type": "SQLFK",
        "fields": [
            { 
                "name":"ab"
            }
        ],
        "reference":{
            "entityName":{
                "prefix":"Rail",
                "name":"ProvinceState"
            },
            "fields":[
                {
                    "name":"Code"
                }
            ]
        }
    }
]

with the below code I'm able to read the foreign key constraints. But I'm struggling to read the "reference" under the SQLFK.

if constraint["name"] ==  "ForeignKeyOne":
    for field in constraint["fields"]:
       fk_attribute_list.append(field["name"])

Please help me on how to read the content "reference".

"reference":{
    "entityName":{
        "prefix":"Rail",
        "name":"ProvinceState"
     },
     "fields":[
          {
              "name":"Code"
          }
     ]
 }
3
  • You read reference in exactly the same way you read name: constraint["reference"]. What, exactly, is giving you trouble? Commented Jul 24, 2015 at 10:35
  • I tried like: for reference in constraint["reference"]: fk_reference_list.append(reference["name"]) The error, im getting, Traceback (most recent call last): File "convert.py", line 57, in <module> fk_reference_list.append(reference["name"]) TypeError: string indices must be integers Commented Jul 24, 2015 at 10:40
  • Well, yes, you can't use it just like if it was a string, since it's an object, it has structure inside. What, exactly, do you want to do with which piece of it? Commented Jul 24, 2015 at 10:43

2 Answers 2

1

You're missing a level of attribute (reference). How about:

if constraint["name"] ==  "ForeignKeyOne":
    for field in constraint["reference"]:
        if field == 'fields':
            for x in constraint["reference"][field]:
                print x

x would contain {'name':'Code'}

That's pretty static, meaning you assume the json structure you have is pretty much the same as above.

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

Comments

0

The "reference" points to a dict. Iterating over a dict yields dict's keys. So in your for reference in constraint["reference"] loop, reference will first yield the string "entityName" then the string "fields". You obviously understand that "somestring"["another_string"] makes no sense, since strings are index-based (integers).

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.