Houy Narun
Houy Narun

Reputation: 1715

How to add new dictionary value using dpath library?

I have kind of python dictionary key value row and columns which look as snippet below. What I want to do is that I want to add a new field 'another_new_field' at row index 0 and column index 0.

I use dpath library to do this and here I what I tried to do

    import dpath.util
    import pprint
    
    
    pp = pprint.PrettyPrinter(indent=2)
    
    
    ext_rows = [ { 'row': [ { 'column': [ { 'col': [ {'class': 'bg-success text-white'},
                                          {'field': ['text01']}]},
                               { 'col': [ {'class': 'bg-info text-white'},
                                          {'field': ['text1']}]},
                               { 'col': [ {'class': 'bg-secondary text-white'},
                                          {'field': ['text2']}]}]}]},
                  { 'row': [ { 'column': [ { 'col': [ {'class': 'bg-primary text-white py-3'},
                                                      {'field': ['text3']}]},
                                           { 'col': [ {'size': 6},
                                                      {'class': 'bg-primary text-white py-3'},
                                                      {'field': ['text3']}]}]}]}]
    
    # at row 0, col0 add new field 'another_new_field'
    
    dpath.util.set(ext_rows, '[0]/row/*/column/0/col/*/field', ["another_new_field"])
    
    pp.pprint(ext_rows)

# RESULT IN:
# [ { 'row': [ { 'column': [ { 'col': [ {'class': 'bg-success text-white'},
#                                     {'field': ['another_new_field']}]},
#                          { 'col': [ {'class': 'bg-info text-white'},
#                                     {'field': ['text1']}]},
#                          { 'col': [ {'class': 'bg-secondary text-white'},
#                                     {'field': ['text2']}]}]}]},
# { 'row': [ { 'column': [ { 'col': [ {'class': 'bg-primary text-white py-3'},
#                                     {'field': ['text3']}]},
#                          { 'col': [ {'size': 6},
#                                     {'class': 'bg-primary text-white py-3'},
#                                     {'field': ['text3']}]}]}]}]

As see in result, it does not append a new field, but it replace the existing value with new field instead. But I want it to be this:

[ { 'row': [ { 'column': [ { 'col': [ {'class': 'bg-success text-white'},
                                    {'field': ['text01','another_new_field']}]},
                         { 'col': [ {'class': 'bg-info text-white'},
                                    {'field': ['text1']}]},
                         { 'col': [ {'class': 'bg-secondary text-white'},
                                    {'field': ['text2']}]}]}]},
{ 'row': [ { 'column': [ { 'col': [ {'class': 'bg-primary text-white py-3'},
                                    {'field': ['text3']}]},
                         { 'col': [ {'size': 6},
                                    {'class': 'bg-primary text-white py-3'},
                                    {'field': ['text3']}]}]}]}]

How can I do that? Thanks.

Upvotes: 0

Views: 179

Answers (1)

Muslimbek Abduganiev
Muslimbek Abduganiev

Reputation: 941

What you want is to get the list at key field and append data to it:

dpath.util.get(ext_rows[0], "/row/*/column/0/col/*/field").append("another_field")

Upvotes: 1

Related Questions