PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 24, 2022

[FIXED] How to formatting a Json content type?

 July 24, 2022     json, jsonforms, python     No comments   

Issue

I need to extract only the content of the operations of the following json:

{"entries":[{"description":"Text transform on 101 cells in column Column 2: value.toLowercase()","operation":{"op":"core/text-transform","engineConfig":{"facets":[],"mode":"row-based"},"columnName":"Column 2","expression":"value.toLowercase()","onError":"keep-original","repeat":false,"repeatCount":10,"description":"Text transform on cells in column Column 2 using expression value.toLowercase()"}},{"description":"Text transform on 101 cells in column Column 6: value.toDate()","operation":{"op":"core/text-transform","engineConfig":{"facets":[],"mode":"row-based"},"columnName":"Column 6","expression":"value.toDate()","onError":"keep-original","repeat":false,"repeatCount":10,"description":"Text transform on cells in column Column 6 using expression value.toDate()"}}]}

It should look like this:

[{"op": "core/text-transform", "engineConfig": {"facets": [], "mode": "row-based"}, "columnName": "Column 2", "expression": "value.toLowercase()", "onError": "keep-original", "repeat": "false", "repeatCount": 10, "description": "Text transform on cells in column Column 2 using expression value.toLowercase()"}, {"op": "core/text-transform", "engineConfig": {"facets": [], "mode": "row-based"}, "columnName": "Column 6", "expression": "value.toDate()", "onError": "keep-original", "repeat": "false", "repeatCount": 10, "description": "Text transform on cells in column Column 6 using expression value.toDate()"}]

I tried to use this code:

import json
operations = [{"description":"Text transform on 101 cells in column Column 2: value.toLowercase()","operation":{"op":"core/text-transform","engineConfig":{"facets":[],"mode":"row-based"},"columnName":"Column 2","expression":"value.toLowercase()","onError":"keep-original","repeat":"false","repeatCount":10,"description":"Text transform on cells in column Column 2 using expression value.toLowercase()"}},{"description":"Text transform on 101 cells in column Column 6: value.toDate()","operation":{"op":"core/text-transform","engineConfig":{"facets":[],"mode":"row-based"},"columnName":"Column 6","expression":"value.toDate()","onError":"keep-original","repeat":"false","repeatCount":10,"description":"Text transform on cells in column Column 6 using expression value.toDate()"}}]
new_operations = []

for operation in operations:
  new_operations.append(operation["operation"])

x = json.dumps(new_operations)
print(x)

However, I must manually place quotes in the words like "fake" and also remove the first "entries" part for it to work. Does anyone know how to do it automatically?


Solution

IIUC you can do it like this. Read it as json data, extract the parts you want and dump it back to json.

with open('your-json-data.json') as j:
    data = json.load(j)

new_data = []
for dic in data['entries']:
    for key,value in dic.items():
        if key == 'operation':
            dic = {k:v for k,v in value.items()}
            new_data.append(dic)

x = json.dumps(new_data)
print(x)

Output:

[
    {
        "op":"core/text-transform",
        "engineConfig":{
            "facets":[              
            ],
            "mode":"row-based"
        },
        "columnName":"Column 2",
        "expression":"value.toLowercase()",
        "onError":"keep-original",
        "repeat":false,
        "repeatCount":10,
        "description":"Text transform on cells in column Column 2 using expression value.toLowercase()"
    },
    {
        "op":"core/text-transform",
        "engineConfig":{
            "facets":[                
            ],
            "mode":"row-based"
        },
        "columnName":"Column 6",
        "expression":"value.toDate()",
        "onError":"keep-original",
        "repeat":false,
        "repeatCount":10,
        "description":"Text transform on cells in column Column 6 using expression value.toDate()"
    }
]


Answered By - Rabinzel
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing