1

Question - I need to convert this json file to csv or excel (xlsx, xls), these all three formats are acceptable.

Input file

Sample:

{"textAnnotations": [{"locale": "en", "description": "KAISER BROTHERS\nWorld\nFood\nSINCE MARTX\n14:25 BEECHNUT STREET\nHouston, TX 77083\n281 988 4566\nTRY OUR CHARGA TODAY!!!\nReceipt #:\n032203\nStore:\nR$1\nTerminal:\n05\nFRE BITTERMELON 1.85 E\nINDIAN LB\n0.930 lb @ $1.99/lb\nBEANS FAVA LB\n2.03 E\n1.020 lb @ $1.99/lb\nFRE TOMATILLO GREEN 0.85 E\nLB\n0.570 lb @ $1.49/lb\nCHILLI LONG LB 2.02 E\n0.580 lb @ $3.49/lb\n5.99 T\nGOD NUPUR HENNA\nMEHENDI 400G\nFRE PURSLANE /KULFA\n1.49 E\nEA\nBABY TARO ARVI LB\n1.16 E\n0.780 lb @ $1.49/lb\nFRE LEMON EA\n0.99 E\n3 @ 0.33\nGINGER ROOT LB\n1.21 E\n0.610 lb @ $1.99/lb\nFRE ONION YELLOW LB\n1.96 E\n3.990 lb @ $0.49/lb\nFRE CABBAGE GREEN\n1.21 E\nLB\n3.680 lb @ $0.33/lb\n2.49 E\n2.49 E\n2.49 E\n2.55 E\n5.99 E\n4.99 E\n1.99 T\n1.99 T\n4.54 E\n6.40 E\n56.68\n0.82\n57.50\n57.50\n57.50\n0.00\n5/12/22 8:36 PM\nClark: HABIB\nMember Name: CHANELLER DEV\nMember ID: 7135578001\n4030\n4239\n3352\n4536\n8901023022548\n3170\n4018\n4109\n4077\n4152\n4069\n051179421158 SWAD ROSE WATER\n440ML\n051179421158 SWAD ROSE WATER\n440ML\n051179421158 SWAD ROSE WATER\n440ML\n4248\nFRE TOMATO ROMA LB\n2.860 lb @ $0.89/lb\n796252300271 YOGURT DRINK MINT\n1/2 GAL\n796252700408 MALAI CREAM 8 OZ\nBREAKFAST\n8954000114797 HEM SAFFRON OIL\n30ML\n8964000114306 HEM CARROT OIL 30ML\n00535\nPPACK CK LEG QRT\nSKINLESS\n3,047 @ 1.49\n00530\nPPACK CK THIGH\nGROUND (KEEMA)\n2.14 @ 2.99\nSUBTOTAL\nSALES TAX\nTOTAL\nCredit\nTOTAL TENDERED\nChange\nCARD INFORMATION:\nXXXXXXXXXXX/XXXXXX\nName:\nCard Type:\nAccount:\nAmount:\nApproval #:\nDate:\nReference #:\nMID:\nMode:\nCard Name: VISA CREDIT\nAID:\nA00000000XXXXX\nVisa\nXXXX\nUSD $57.50\n86018D\n5/12/22\n001032203001\n**1620\nIssuer\nToday you saved $1.35\nThank you for shopping with us!\nFINAL SALE, FINAL SALE, FINAL SALE\nNo return or exc

I had get this file from google vison API,

Expecting output file. In particularly, table2 data was expected data.

both output formats are acceptable

sample output data format 1

left center right right
4030 FRE BITTERMELON INDIAN LB 0.930 1b 0 $1.99/lb 1.85 E
4239 BEANS FAVA LB 1.020 1b 0 $1.99/lb 2.03 E

sample output data format 2

left center right
4030 FRE BITTERMELON INDIAN LB 0.930 1b 0 $1.99/lb 1.85 E
4239 BEANS FAVA LB 1.020 1b 0 $1.99/lb 2.03 E

sample output image format 2

detailed output please check above mentioned link

I was try these two types of library's, but output was not getting in a expected format, anyone can help me here.

references - link

import json
import pandas as pd
# Opening JSON file
f = open('data.json')
  
# returns JSON object as 
# a dictionary
data = json.load(f)
  
# Iterating through the json
# list
a = data['textAnnotations'][0]['description']
data = {'A': a}
#data = {'A': a[0:]}
df = pd.DataFrame.from_dict([data])

output getting data in a single cell

Another method i was try

import pandas as pd
# As of Pandas 1.01, json_normalize as pandas.io.json.json_normalize is deprecated and is now exposed in the top-level namespace.
# from pandas.io.json import json_normalize
from pathlib import Path
import json

# set path to file
p = Path(r'data.json')

# read json
with p.open('r', encoding='utf-8') as f:
    data = json.loads(f.read())

# create dataframe
df = pd.json_normalize(data)

# save to csv
#df.to_csv('test.csv', index=False, encoding='utf-8')

output getting data in a single row

NFR
  • 25
  • 5
  • can you provide a sample of the file (without having to leave SO) – Umar.H Jun 23 '22 at 13:57
  • [link] (https://drive.google.com/file/d/1m83i9NgtC1lDsuPp_Mlgk-GBz02K1PI_/view) this is a sample file. thank you for your support. sorry, not getting you point, from this sentence (without having to leave SO) – NFR Jun 23 '22 at 14:00
  • basically a user who wants to answer a question should not have to leave stackoverflow to help, provide the file as text in your question – Umar.H Jun 23 '22 at 14:08
  • sorry, Unable to add input file due to character exceeded warning was shown by stack overflow. – NFR Jun 23 '22 at 14:38
  • don't add the whole file. a few sample *text* lines (not images) is fine. – Mark Tolonen Jun 23 '22 at 15:09

0 Answers0